Home > Net >  Why am I getting this deprecated warning and why I can't modify my collection!? MongoDB,NODE JS
Why am I getting this deprecated warning and why I can't modify my collection!? MongoDB,NODE JS

Time:03-08

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:\coursera\Node JS\node-mongo>npm start

> [email protected] 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. (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.

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?

Error Image

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.

CodePudding user response:

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

CodePudding user response:

(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

  • Related