Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

【 Mongo series 】mongodb learning three, common operation practice

Insert data

> db.users.insert({"name" : "xiaomotong"."age"18}) :

> db.users.insert({name:"xiaozhu",age:15,hobby:"basketball",infos:{tall:190,height:70},school:"sh"})

> db.users.insertMany([{name:"xiaopang"},{name:"wangwu"}])

> db.users.insertMany([{name:"nancy"."age": 25."hobby" : "study"."infos" : { "tall" : 175, "height": 60},"school" : "hn" },{name:"job"."age" : 19, "hobby" : "basketball"."infos" : { "tall" : 170, "height": 70}."school" : "nj" }])
Copy the code
  • You can use insert, insertOne, and insertMany to insert different data, each as needed, where insertMany is used to insert multiple data, of course, can also insert one data

There are so many other methods mongodb can use to insert data, depending on your needs

Update the data


> db.users.find()
{ "_id" : ObjectId("61584aeeee74dfe04dac57e9"), "name" : "xiaomotong", "age" : 18 }
...

> db.users.update({name:"xiaomotong"}, {$set:{name:"xiaokeai",age:25,hobby:"reading",infos:{tall:175,height:62},school:"cs"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Copy the code

Using $set indicates that the field needs to be updated

When querying the data, we found that mongodb automatically added the _id field to our document, which is a primary key. If we do not set it ourselves, mongodb will give us a 24-bit UUID by default

The official website also provides these methods for updating:

To find the data

> db.users.find({"infos.tall": {$gt: 170}})
{ "_id" : ObjectId("61584aeeee74dfe04dac57e9"), "name" : "xiaokeai", "age" : 25, "hobby" : "reading", "infos" : { "tall" : 175, "height" : 62 }, "school" : "cs" }
{ "_id" : ObjectId("615a56d6bc6afecd2cff8f96"), "name" : "xiaozhu", "age" : 15, "hobby" : "basketball", "infos" : { "tall" : 190, "height" : 70 }, "school" : "sh" }
{ "_id" : ObjectId("615a5917d988690b07c69f66"), "name" : "nancy", "age" : 25, "hobby" : "study", "infos" : { "tall" : 175, "height" : 60 }, "school" : "hn" }

Copy the code

$gt = > $gt = > $gt =

Tall is a subquery, which means infos is an embedded document, and the tall field in the document needs to be searched, so we have to use double quotation marks, because the system will recognize infos. Tall as a field by default, and in fact, this field cannot be found, so the query result cannot be achieved. Error:

> db.users.find({infos.tall:{$gt: 170}})2021-10-04T09:39:21.349+0800 E QUERY [js] Uncaught exception: SyntaxError: missing: after property ID: @(shell):1:20Copy the code

Query data presented in a more user-friendly way

> db.users.find({"infos.tall": {$gte:180}}).pretty()
{
        "_id" : ObjectId("615a56d6bc6afecd2cff8f96"),
        "name" : "xiaozhu",
        "age" : 15,
        "hobby" : "basketball",
        "infos" : {
                "tall" : 190,
                "height" : 70
        },
        "school" : "sh"
}
Copy the code

Db.users. find({“infos.tall”:{$gte:180}}) select * from users where “infos.tall” >= 180

For example, query the student whose height is 175 or 190.

Db.users. find({“infos.tall”:{$in:[190,175]}}) select * from users where “infos.tall” in (190,175)

To demonstrate a simple regular expression:

> db.users.find({name:/^w/}).pretty()
{ "_id" : ObjectId("615a5856d988690b07c69f65"), "name" : "wangwu" }
> db.users.find({name:/g$/}).pretty()
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
Copy the code
  • Search for wangwu (name, w)
  • Query the name resource, match the result at the end of G, and match xiaotong

Here, when we query, / regular expression /, mongodb will automatically recognize and match and process according to the rules of regular expression

We can also query for NULL

> db.users.find({infos:null})
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
{ "_id" : ObjectId("615a5856d988690b07c69f65"), "name" : "wangwu" }

> db.users.find({infos:{$exists:false}})
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
Copy the code

If the query field is null, you can use the first method. If the query field does not exist, you can use the second method

Delete the data

> db.users.deleteOne({name:"wangwu"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.users.find({name:/^w/}).pretty()
>
Copy the code

Common deletion methods are as follows:

  • db.users.deleteOne()
  • db.users.deleteMany()
  • db.users.remove()

Simple paging query

> use mytest
switched to db mytest
> db.users.find()
{ "_id" : ObjectId("61584aeeee74dfe04dac57e9"), "name" : "xiaokeai", "age" : 25, "hobby" : "reading", "infos" : { "tall" : 175, "height" : 62 }, "school" : "cs" }
{ "_id" : ObjectId("615a56d6bc6afecd2cff8f96"), "name" : "xiaozhu", "age" : 15, "hobby" : "basketball", "infos" : { "tall" : 190, "height" : 70 }, "school" : "sh" }
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
{ "_id" : ObjectId("615a5917d988690b07c69f66"), "name" : "nancy", "age" : 25, "hobby" : "study", "infos" : { "tall" : 175, "height" : 60 }, "school" : "hn" }
{ "_id" : ObjectId("615a5917d988690b07c69f67"), "name" : "job", "age" : 19, "hobby" : "basketball", "infos" : { "tall" : 170, "height" : 70 }, "school" : "nj" }

> db.users.find().limit(2)
{ "_id" : ObjectId("61584aeeee74dfe04dac57e9"), "name" : "xiaokeai", "age" : 25, "hobby" : "reading", "infos" : { "tall" : 175, "height" : 62 }, "school" : "cs" }
{ "_id" : ObjectId("615a56d6bc6afecd2cff8f96"), "name" : "xiaozhu", "age" : 15, "hobby" : "basketball", "infos" : { "tall" : 190, "height" : 70 }, "school" : "sh" }

> db.users.find().skip(2).limit(2)
{ "_id" : ObjectId("615a5856d988690b07c69f64"), "name" : "xiaopang" }
{ "_id" : ObjectId("615a5917d988690b07c69f66"), "name" : "nancy", "age" : 25, "hobby" : "study", "infos" : { "tall" : 175, "height" : 60 }, "school" : "hn" }

Copy the code
  • db.users.find()Query all the data and print it in the default order
  • db.users.find().limit(2)From the previous result, limit two items
  • db.users.find().skip(2).limit(2)From the previous result, offset 2 and restrict 2

Next time, comb through the aggregate content

The official firsthand material: docs.mongodb.com/manual/refe…

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~