To achieve this we will use the MongoDB driver (
const MongoClient = require('mongodb').MongoClient;
// Connection url
const url = 'mongodb://192.168.1.203:27017';
options = {
useNewUrlParser: true
};
MongoClient.connect(url, options).then(function (ok) {
db1 = ok;
dbName = 'testdb';
return db1.db(dbName).collection('test').updateOne({"this":1},{$set:{"another":"this is a test"}});
})
.then(function (dbs) {
console.log(dbs);
db1.close();
});
In mongo shell, we get the following result:
MongoDB Enterprise > db.test.find()
{ "_id" : ObjectId("5bab60e785f572e55fe09165"), "this" : 1, "another" : "this is a test1" }
{ "_id" : ObjectId("5bab61018df000e560655d2d"), "this" : 1, "another" : "this is a test" }
{ "_id" : ObjectId("5bab6110b8724ae561d7191d"), "this" : 1, "another" : "this is a test" }
Here is the object “dbs” that is returned.
CommandResult {
result: { n: 1, nModified: 1, ok: 1 },
connection:
Connection {
domain: null,
_events:
{ error: [Object],
close: [Object],
timeout: [Object],
parseError: [Object] },
_eventsCount: 4,
_maxListeners: undefined,
options:
{ host: '192.168.1.203',
port: 27017,
size: 5,
minSize: 0,
connectionTimeout: 30000,
socketTimeout: 360000,
keepAlive: true,
keepAliveInitialDelay: 300000,
noDelay: true,
ssl: false,
checkServerIdentity: true,
ca: null,
crl: null,
cert: null,
key: null,
passPhrase: null,
rejectUnauthorized: false,
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
reconnect: true,
reconnectInterval: 1000,
reconnectTries: 30,
domainsEnabled: false,
disconnectHandler: [Object],
cursorFactory: [Object],
emitError: true,
monitorCommands: false,
promiseLibrary: [Function: Promise],
clientInfo: [Object],
servers: [Array],
useNewUrlParser: true,
db: 'admin',
dbName: 'admin',
socketTimeoutMS: 360000,
connectTimeoutMS: 30000,
bson: BSON {} },
id: 0,
logger: Logger { className: 'Connection' },
bson: BSON {},
tag: undefined,
messageHandler: [Function],
maxBsonMessageSize: 67108864,
port: 27017,
host: '192.168.1.203',
family: undefined,
keepAlive: true,
keepAliveInitialDelay: 300000,
noDelay: true,
connectionTimeout: 30000,
socketTimeout: 360000,
destroyed: false,
domainSocket: false,
singleBufferSerializtion: true,
serializationFunction: 'toBinUnified',
ca: null,
crl: null,
cert: null,
key: null,
passphrase: null,
ciphers: null,
ecdhCurve: null,
ssl: false,
rejectUnauthorized: false,
checkServerIdentity: true,
responseOptions:
{ promoteLongs: true,
promoteValues: true,
promoteBuffers: false },
flushing: false,
queue: [],
connection:
Socket {
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: null,
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
_bytesDispatched: 488,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
_idleTimeout: 360000,
_idleNext: [Object],
_idlePrev: [Object],
_idleStart: 307,
_destroyed: false,
[Symbol(asyncId)]: 5,
[Symbol(bytesRead)]: 0,
[Symbol(asyncId)]: 8,
[Symbol(triggerAsyncId)]: 1 },
writeStream: null,
hashedName: 'ecc4c59c3c6dde71259f7aabc040337c19f4b64c',
workItems: [],
buffer: null,
sizeOfMessage: 0,
bytesRead: 0,
stubBuffer: null },
message:
Response {
parsed: true,
raw: <Buffer 4b 00 00 00 ed 61 00 00 02 00 00 00 01 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 27 00 00 00 10 6e 00 01 00 00 00 10 6e 4d ... >,
data: <Buffer 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 27 00 00 00 10 6e 00 01 00 00 00 10 6e 4d 6f 64 69 66 69 65 64 00 01 00 00 00 01 6f 6b 00 ... >,
bson: BSON {},
opts:
{ promoteLongs: true,
promoteValues: true,
promoteBuffers: false },
length: 75,
requestId: 25069,
responseTo: 2,
opCode: 1,
fromCompressed: undefined,
responseFlags: 8,
cursorId: Long { _bsontype: 'Long', low_: 0, high_: 0 },
startingFrom: 0,
numberReturned: 1,
documents: [ [Object] ],
cursorNotFound: false,
queryFailure: false,
shardConfigStale: false,
awaitCapable: true,
promoteLongs: true,
promoteValues: true,
promoteBuffers: false,
index: 59,
hashedName: 'ecc4c59c3c6dde71259f7aabc040337c19f4b64c' },
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0,
matchedCount: 1 }