MongoDB Learning Notes - Addition, deletion and modification of documents in Mongo Shell

Keywords: PHP MongoDB Database shell

MongoDB Learning Notes (2) - Documentation Addition, Deletion and Amendment in Mongo Shell

The version of MongoDB used in this article is 4.0.10

> db.version();
4.0.10

I. Insert Documents

1. Insert a document

Syntax: db. < collection >. insert (document)

Insert a document into the user collection in the test database:

> use test;
switched to db test

> db.user.insert({ "username" : "Tom", "age" : 10 })
WriteResult({ "nInserted" : 1 })

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 10 }

Note: _id field is automatically generated by the system, you can also specify any type of word, but the value can not be repeated.

2. Insert multiple documents

Syntax: db. < Collection > insert (document1, document2,..., documentN])

Insert multiple documents into the user collection in the test database:

> db.user.insert([
...     { "username" : "Mary", "age" : 30 },
...     { "username" : "Martin", "age" : 40 },
...     { "username" : "kart", "age" : 50 },
...     { "username" : "Jack", "age" : 20 }
... ])
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 4,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 10 }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
{ "_id" : ObjectId("5d2f11b814077ad0dab139ca"), "username" : "Jack", "age" : 20 }

2. Delete data

Grammar: db. < collection >. remove (condition)

Delete a document whose name equals "Jack" in the user collection

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 10 }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
{ "_id" : ObjectId("5d2f11b814077ad0dab139ca"), "username" : "Jack", "age" : 20 }

> db.user.remove({ "username" : "Jack" })
WriteResult({ "nRemoved" : 1 })

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 10 }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

III. Modification of data

Syntax: update

Modify the age of 10 to 20 in the user set

Method 1:

var u = db.user.findOne( { "age" : 10 } );
u.age = 20;
db.user.update( { "age" : 10 } , u ) or db.user.save(u);
> var u = db.user.findOne({ "age" : 10 })

> u.age = 20
20

> db.user.save(u)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 20 }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

Method 2:

db.user.update( query, object[, upsert_bool, multi_bool] )
> var u = db.user.findOne({ "age" : 20 })

> u.age = 10
10

> db.user.update({ "age" : 20 }, u)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 10 }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }

upsert_bool: If there is no record that meets the query criteria, create the record

Note: If more than one record satisfies {age":10}, only the first record will be modified. The fourth parameter can be set to true to modify all records.

Error-prone areas when modifying data

If such data are available:

{"_id" : 1 , "username" : "abc" , "age" : 20 , "sex" : "boy" }

To modify the age of 22, the error code is as follows:

var u = db.user.update({ "_id" : 1 }, { "age" : 22 })

After the record has been modified, it becomes:

{ "_id" : 1 , "age" : 22 }

The right way:

Var u = db. user. find ({"_id": 1}); --> take out records
 U.age = 22; - > Basically modified in the original record
 db.user.save(u) or db. user. update ({"_id": 1}, u)

IV. Use of Modifiers

1. $inc: Add a number

Add Tom's age to 2

> db.user.update({ "username" : "Tom" }, { $inc : { "age" : 2 } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find({ "username" : "Tom" })
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12 }

2. $set: Modify a field and add it if it does not exist

Modify Tom's phone number to 10086 and add this field if there is no field

> db.user.update({ "username" : "Tom" }, { $set : { "tel" : "10086" } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find({ "username" : "Tom" })
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }

Modify arrays

3. $push: Adding elements to an array

Add a friend Jack to Tom's friends.

> db.user.update({ "username" : "Tom" }, { $push: { "friend" : "Jack" } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find({ "username": "Tom"}, { "username": 1, "friend": 1 })
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "friend" : [ "Jack" ] }

4. $each: Traversing operation elements

Add "Mary" and "Jocker" to Tom's friends in batches.

> db.user.update({ "username" : "Tom" }, { $push : { "friend" : { $each : ["Mary", "Jocker"] } } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find({ "username" : "Tom" }, { "username" : 1, "friend" : 1 })
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "friend" : [ "Jack", "Mary", "Jocker" ] }

5. $pop: Extract data from the beginning or end of an array

Delete the Last Friend from abc's Friends

> db.user.update({ "username" : "Tom" }, { $pop : { "friend" : 1 }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find({ "username" : "Tom"}, { "username" : 1, "friend" : 1})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "friend" : [ "Jack", "Mary" ] }

Delete the first friend from abc's friends

> db.user.update({ "username" : "Tom" }, { $pop : { "friend": -1 } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find({ "username" : "Tom" }, { "username" : 1, "friend" : 1})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "friend" : [ "Mary" ] }

6. $unset: Delete a field

Delete Tom's friend field

> db.user.update( { "username" : "Tom" }, { $unset : { "friend" : "" } } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.user.find({ "username" : "Tom" })
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }

Modifications, deletions and updates in MongoDB are instantaneous, that is, the client only sends the command to the server, but does not check whether the command is successfully executed.
You can check for success by executing getLastError after each command has been executed!

> db.runCommand({"getLastError": 1})
{
        "connectionId" : 1,
        "n" : 0,
        "syncMillis" : 0,
        "writtenTo" : null,
        "err" : null,
        "ok" : 1
}

Posted by lamajlooc on Tue, 30 Jul 2019 04:33:17 -0700