Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

315
Views
¿Cómo escribir la salida del script nodejs en mongo?

Quiero hacer un script de nodejs, debería hacer ping al host y escribir el resultado en mongodb. Ahora estoy usando la agenda para crear un trabajo y comenzar un ping en el momento adecuado. ¿Cómo escribir el resultado de la ejecución en la base de datos? Traté de usar Mongoclient, pero me parece que esto no es un poco lo que necesito.

 var ping = require('ping'); const mongo = require('mongodb'); const MongoClient = mongo.MongoClient; const url = 'mongodb://127.0.0.1:27017/test'; const config = ['10.10.4.45'] const Agenda = require('agenda'); const dbURL = 'mongodb://127.0.0.1:27017/test'; const agenda = new Agenda({ db: { address: dbURL, collection: 'jobs', options: { useNewUrlParser: true, useUnifiedTopology: true } } }); let counter = 0; agenda.define('Ping host 10.10.4.45', async job => { counter++; await console.log('Ping test #'+counter); for(let host of config){ let res = await ping.promise.probe(host); console.log(res.host, res.alive); } }); (async function() { await agenda.start(); await agenda.every('5 seconds', 'Ping host 10.10.4.45'); })();

 MongoClient.connect(url, function(err, db) { if (err) throw err; var dbo = db.db("test"); var myobj = [ { name: 'John', address: 'Highway 71'} ]; dbo.collection("ping_results").insertMany(myobj, function(err, res) { if (err) throw err; console.log("Number of documents inserted: " + res.insertedCount); db.close(); }); });
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Guarde el siguiente código en un archivo, digamos ping_results.js dentro de su repositorio en una carpeta seeding_scripts por ejemplo, y luego ejecútelo usando el siguiente comando:

 node seeding_scripts\ping_results.js

archivo ping_results.js .

 const MongoClient = require('mongodb').MongoClient; const DB_URI = "mongodb://localhost:27017/db_name"; const options = { useNewUrlParser: true }; MongoClient.connect(DB_URI, options, function (err, client) { if (err) { console.log("ERROR: Failed to connect to database."); console.log(err); return; } let dbName = DB_URI.split("/", -1).pop(); let collectionName = "ping_results"; let db = client.db(dbName); console.log(`Connected to ${dbName} database successfully.`); var data = [ { name: 'John', address: 'Highway 71' } ]; db .collection(collectionName) .insertMany(data) .then(res => { console.log(`${res.insertedCount} Documents inserted successfully.`); console.log("Database connection closed."); client.close(); }) .catch(err => { console.log(JSON.stringify(err)); console.log("Database connection closed."); client.close(); }); });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!