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" }])
- You can use insert, insertOne, and insertMany to insert different data, each takes what you need, of which insertMany is used to insert multiple pieces of data, of course, you can also insert 1 piece of data
mongodb has so many functions and methods that can be used in inserting data.
update 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 })
Use $set to indicate that the field needs to be updated
When querying data, we found that mongodb automatically added the _id field to our document, which is a primary key. If you don't set it yourself, mongodb adds it to us by default, which is a 24-bit uuid
Regarding the update method, the official website also provides these methods:
Find 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" }
$gt here means greater than. I mentioned these operators in the last issue, so let's review them again
The above infos.tall
belongs to a subquery, indicating that infos is an embedded document, and what needs to be searched is the tall field in the document. We can't add double quotation marks, because the system will recognize infos.tall as a field by default. In fact, this field cannot be found, so the query result cannot be obtained, and the following error is reported:
> 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:20
Query data in a more 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"
}
Here db.users.find({"infos.tall":{$gte:180}})
is similar to the SQL statement of relational database: select * from users where "infos.tall" >= 180
For example, in a query, students whose height is 175 or 190.
Here db.users.find({"infos.tall":{$in:[190,175]}})
is similar to the relational database select * from users where "infos.tall" in (190,175)
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" }
- Query the name field, match the results starting with w, and match wangwu
- Query the name resource, match the result ending with g, and match it out xiaotong
Here, when we query, /regular expression/, mongodb will automatically recognize and match and process according to the rules of regular expressions
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" }
The first method can be used if the query field is null, and the second method can be used if the query field does not exist
delete data
> db.users.deleteOne({name:"wangwu"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.users.find({name:/^w/}).pretty()
>
Commonly used deletion methods are:
- db.users.deleteOne()
- db.users.deleteMany()
- db.users.remove()
Simple pagination 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" }
-
db.users.find()
Query all data and print them out in the default order -
db.users.find().limit(2)
From the previous results, the limit is 2 -
db.users.find().skip(2).limit(2)
From the previous result, offset by 2, and limit to take out 2
Next time, sort out and aggregate related content
Official first-hand information: https://docs.mongodb.com/manual/reference
Welcome to like, follow, favorite
Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality
Okay, here it is this time
Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.
I am the native of Abingyun , welcome to like, follow and collect, see you next time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。