I’m working with MongoDB in NodeJS,
const MongoClient=require('mongodb').MongoClient; const assert=require('assert'); const dbupper=require('./operations'); const url='mongodb://localhost:27017'; const dbname='conFusion'; MongoClient.connect(url).then((client,err)=>{ assert.equal(err,null); const db =client.db(dbname); console.log('Connected correctly to the server'); dbupper.insertDocument(db,{name:'ansh',description:'text'},'dishes') .then((result)=>{ console.log("Insert Document:n",result.ops); return dbupper.findDocument(db,'dishes'); }) .then((docs)=>{ console.log('Found Documents:n',docs); return dbupper.updateDocument(db,{name:'ansh'},{description:'updates text'},'dishes'); }) .then((result)=>{ console.log('Updated Documents:n',result.result); return dbupper.findDocument(db,'dishes'); }) .then((docs)=>{ console.log('Found Documents:n',docs); return db.dropCollection('dishes') }) .then((result)=>{ console.log('Dropped Collection: ',result); client.close(); }) .catch((err)=> console.log(err)); }) .catch((err)=> console.log(err));
Message shows:
C:courseraNode JSnode-mongo>npm start > <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f09e9f9495dd9d9f9e979fb0c1dec0dec0">[email protected]</a> start > node index
(node:10780) [MONGODB DRIVER] Warning: Current Server Discovery and
Monitoring engine is deprecated, and will be removed in a future
version. To use the new Server Discover and Monitoring engine, pass
option { useUnifiedTopology: true } to the MongoClient constructor.
(Usenode --trace-warnings ...
to show where the warning was created)
Connected correctly to the server
(node:10780) DeprecationWarning: collection.insert is deprecated. Use insertOne, insertMany or bulkWrite instead.
Insert Document: [ { name: 'ansh', description: 'text', _id: 6226041b2291342a1c121a01 } ] Found Documents: [ { _id: 6226041b2291342a1c121a01, name: 'ansh', description: 'text' } ] Updated Documents: { n: 1, nModified: 1, ok: 1 } Found Documents: [ { _id: 6226041b2291342a1c121a01, name: 'ansh', description: 'updates text' } ] Dropped Collection: true
But I’m not using any deprecated options. Any ideas?
EDIT: I can’t modify my collection, even if I do anyone of the operations there are no changes to the collection. From what I can gather I can’t connect to the MongoClient successfully.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
you tried with this update query.
dbupper.updateDocument(db,{name:'ansh'},{description:'updates text'},'dishes')
if you tried with
dbupper.updateOne(db,{name:'ansh'},{"$set":{description:'updates text'}},'dishes')
might be it’s helpful to you to get the desired result.
for more information you can refer this link mongo update
Method 2
(node:10780) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. (Use node –trace-warnings … to show where the warning was created) Connected correctly to the server (node:10780) DeprecationWarning: collection.insert is deprecated. Use insertOne, insertMany or bulkWrite instead.
for this deprecation might be you need a pass option on the mongo connection method. or use recommended latest for mongo commands and queries.
MongoClient.connect(url,{ useUnifiedTopology: true }).then((client,err)=>{ assert.equal(err,null); const db =client.db(dbname); }))
try the above way to connect your database
Method 3
The connectivity issue solved itself when I uninstalled and reinstalled mongodb but the warnings remained which was solved by
(url,{ useUnifiedTopology: true }
inside the MongoClient.connect as stated by RONAK SUTARIYA.
Now I could modify the database without any problems.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0