In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The purpose of this article is to share with you what are the common sentences in MongoDB. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
If you find the Mongodb statement difficult to understand, you can compare it with the SQL statement, which is much easier to learn.
1. Query (find)
Query all results
Select * from articledb.article.find ()
Specify which keys to return
Select title, author from articledb.article.find ({}, {"title": 1, "author": 1})
Where condition
Select * from article where title = "mongodb" db.article.find ({"title": "mongodb"})
And condition
Select * from article where title = "mongodb" and author = "god" db.article.find ({"title": "mongodb", "author": "god"})
Or condition
Select * from article where title = "mongodb" or author = "god" db.article.find ({"$or": [{"title": "mongodb"}, {"author": "god"}]})
Comparative condition
Find ({"read": {"$gt": 100}}) > $gt (>), $gte (> =), $lt (Note: the fields corresponding to $inc must be numeric, and the incrementing or decreasing values must also be numeric.
Array append ($push):
Db.game.update ({"_ id": 123}, {"$push": {"score": 123}})
You can also append multiple elements at a time:
Db.game.update ({"_ id": 123}, {"$push": {"score": [12123]}})
Note: the append field must be an array. If the array field does not exist, it is automatically added and then appended.
Append multiple elements at a time ($pushAll):
Db.game.update ({"_ id": 123}, {"$pushAll": {"score": [12123]}})
Append non-repeating elements ($addToSet):
$addToSet is similar to the collection Set, increasing only when this value is not within the element:
Db.game.update ({"_ id": 123}, {"$addToSet": {"score": 123}})
Delete the element ($pop):
Db.game.update ({"_ id": 123}, {"$pop": {"score": 1}) / / Delete the last element db.game.update ({"_ id": 123}, {"$pop": {"score":-1}}) / / Delete the first element
Note: $pop can only delete one element in the array at a time. 1 means to delete the last one, and-1 means to delete the first one.
Delete a specific element ($pull):
Db.game.update ({"_ id": 123}, {"$pull": {"score": 123}})
The above statement denotes the deletion of elements in the array whose score value is equal to 123.
Delete multiple specific elements ($pullAll):
Db.game.update ({"_ id": 123}, {"$pullAll": {score: [123 pullAll 12]}})
The above statement means to delete elements in the array whose value is equal to 123 or 12.
Update the value of the nested array:
Use array subscript (starting at 0):
{address: [{place: "nanji", tel: 123}, {place: "dongbei", tel: 321}]} db.game.update ({"_ id": 123}, {"$set": {"address.0.tel": 213}})
If you don't know which item of the array to update, we can use the $operator ($represents itself, that is, the item in the array found according to the query condition, and only the first array item found will be applied):
Db.game.update ({"address.place": "nanji"}, {"$set": {"address.$.tel": 123}})
In the above statement, $is the query result of the query condition {"address.place": "nanji"}, that is, {place: "nanji", tel: 123}, so {"address.$.tel": 123} is {"address. {place:" nanji ", tel: 123} .tel": 123}.
4. Delete (remove)
Delete all documents:
Delete from articledb.article.remove ()
Delete the specified document:
Delete from article where title = "mongodb" db.article.remove ({title: "mongodb"}) Thank you for reading! This is the end of this article on "what sentences are commonly used in MongoDB?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.