'use strict';
var mongoClient = require('mongodb').MongoClient,
assert = require('assert');
// Form the URI
var url = 'mongodb://mongouser:thepasswordA1@10.66.161.177:27017/admin';
mongoClient.connect(url, function(err, db) {
assert.equal(null, err);
var db = db.db('testdb'); // Select a database
var col = db.collection('demoCol'); // Select a collection (table)
// Insert data
col.insertOne(
{
a: 1,
something: "yy"
},
// Optional parameters
//{
// w: 'majority' // Enable the "Majority" mode to ensure that data are written to the Secondary nodes
//},
function(err, r) {
console.info("err:", err);
assert.equal(null, err);
// Assertion is written successfully
assert.equal(1, r.insertedCount);
// Query data
col.find().toArray(function(err, docs) {
assert.equal(null, err);
console.info("docs:", docs);
db.close();
});
}
);
});
Was this page helpful?