1

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?

smartmouse
  • 13,912
  • 34
  • 100
  • 166
  • Do you want to delete the `myList` collection or to delete the first document in the `myList` collection ? – Titus Sep 22 '19 at 17:51
  • The first element in the `myList` collection. – smartmouse Sep 22 '19 at 18:04
  • 1
    That means you want to delete a document not an element (a field), the way you use the `update` function is for removing an array element from a document field. Take a look at https://docs.mongodb.com/manual/tutorial/remove-documents/ – Titus Sep 22 '19 at 18:07
  • 1
    There is [no thing like a "first document in a collection" in MongoDB](https://stackoverflow.com/questions/11599069/how-does-mongodb-sort-records-when-no-sort-order-is-specified). – str Sep 22 '19 at 18:09
  • Also, I think you are confusing the MongoDB syntax with the [Node.js driver for MongoDB](https://docs.mongodb.com/ecosystem/drivers/node/). – str Sep 22 '19 at 18:14
  • I edited my question according to your hints, thank you. So, there is no "pop" functions for documents? – smartmouse Sep 22 '19 at 19:33

1 Answers1

1

The following code can do the trick:

// Getting the first document
var firstDoc = db.collection.findOne();

// Deleting the first document
db.collection.remove(firstDoc["_id"]);

Before removal:

{ "_id" : ObjectId("5d88201138db7cf8d3f75cd1"), "value" : 1 }
{ "_id" : ObjectId("5d88201138db7cf8d3f75cd2"), "value" : 2 }
{ "_id" : ObjectId("5d88201138db7cf8d3f75cd3"), "value" : 3 }
{ "_id" : ObjectId("5d88201138db7cf8d3f75cd4"), "value" : 4 }
{ "_id" : ObjectId("5d88201138db7cf8d3f75cd5"), "value" : 5 }

After removal:

{ "_id" : ObjectId("5d88201138db7cf8d3f75cd2"), "value" : 2 }
{ "_id" : ObjectId("5d88201138db7cf8d3f75cd3"), "value" : 3 }
{ "_id" : ObjectId("5d88201138db7cf8d3f75cd4"), "value" : 4 }
{ "_id" : ObjectId("5d88201138db7cf8d3f75cd5"), "value" : 5 }
Himanshu Sharma
  • 2,940
  • 1
  • 7
  • 18