I'm developing an app and I'm using for the first time MongoDB.
I'm experiencing some problems with usage, because documentation seems to be outdated and even the answers here seem to be related to previous version, so several functions are deprecated.
I would like to get and then delete the first document of my database.
I have tried the following code:
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
const url = <my mongodb url>;
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) {
if (err) {
console.error('Unable to connect to the mongoDB server. Error:', err);
} else {
const db = client.db('myFirstDb').collection('myList');
db.find().toArray((err, items) => {
console.log(items.length)
});
db.updateOne({}, {$pop: {myList: -1}}, function (err, result) {
console.log('Done');
});
client.close();
}
});
But nothing happens. The docs makes this example:
db.students.update( { _id: 1 }, { $pop: { scores: 1 } } )
but it deletes an element of an array of one document in the db; in my case I need to pop the first entry of my db. Anyway, as I said the docs is not updated, because it uses update
function that is deprecated.
Do you know if is it possible to do what I need?