In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
(1) increase:
Db.collname.insert ({name: "user1", age: "20"}) add a piece of regular data
The added numbers are key-value pairs in json format, and the subsequent values need to be in double quotation marks or quotation marks
Db.collname.insert ({time:new Date ()}) mongodb supports js, and you can add js statements to query
Db.collname.insert ({name: ["user1", "user2", "user3"]}); add an array of data
Db.collname.insert ({name: {first: "user1", middle: "user2", last: "user3"}}) add a piece of data with an embedded document, (a row of data in mongodb is also known as a document)
Custom document insertion:
> doc= {
... Name: "user"
... Age: "20"
... Sexy: "man"
...}
{"name": "user", "age": "20", "sexy": "man"}
> db.c1.insert (doc)
(2) check
View all the data of collection (C1)
Db.c1.find ()
View the data with the user name smith
Db.c1.find (name: "smith")
View all data except the age field
Db.c1.find ({}, {age:0})
Check that the user name is smith, excluding other fields in the age field.
Db.c1.find ({name: "smith"}, {age:0})
Only view the age field whose user name is smith
Db.c1.find ({name: "smith"}, {age:1})
View data over the age of 20
Db.c1.find ({age: {$gt:20}})
View data under the age of 20
Db.c1.find ({age: {$lt:20}})
View data older than or equal to 20
Db.c1.find ({age: {$gte:20}})
View data whose age is less than or equal to 20
Db.c1.find ({age: {$lte:20}})
View data that are not equal to 20 years old
Db.c1.find ({age: {$ne:20}})
View data with both singging and football hobbies
Db.c1.find ({hobby: {$all: ["singging", "football"]}}); ($all must include all, and $in needs to include only one of them)
Check data with singging or football hobbies.
Db.c1.find ({hobby: {$in: ["singging", "football"]}})
Check the data without singging or football hobbies.
Db.c1.find ({hobby: {$nin: ["singging", "football"]}})
View the data where a field exists: output only five pieces of data
Db.c1.find ({name: {$exists:true}}) .limit (5)
Take a model to view the data with a Mantissa of age
Db.c1.find ({age: {$mod: [10Jing 1]}})
View data that meet at least one requirement $or
Db.c1.find ({$or: [{age:20}, {sexy: "man"})
View a data that does not meet the requirements $nor
Db.c1.find ({$nor: [{age:20}, {sexy: "man"})
View data with an array length equal to 3 (set an as an array)
Db.c1.find ({a: {$size:3}})
Retrieve all users whose name attribute begins with u and ends with 4 (regular expressions can be used)
Db.c1.find ({name:/u.*4 $/ I})
Sort / / 1 for ascending order,-1 for descending order
Db.c1.find () .sort ({_ id: 1})
Check the unique value.
Db.c1.distinct ("age")
Paging query:
Db.c1.find () .skip (20) .limit (8)
Or: db.c1.find ({}, {}, 8pm 20)
Number of query records:
Db.c1.find () .count ()
Query a document that does not have a value or does not exist:
Db.c1.find (name:null)
The usage of $slice in query
Db.posts.find ({}, {comments: {$slice: 5}}) / / the first five comments
Db.posts.find ({}, {comments: {$slice:-5}}) / / the last 5 comments
Db.posts.find ({}, {comments: {$slice: [20,10]}) / / skip 20, limit 10
Db.posts.find ({}, {comments: {$slice: [- 20,10]}) / / 20 from end, limit 10
(3) deletion
Delete all data under C1 (collection)
Db.c1.remove ()
Delete data with an id of 10
Db.c1.remove ({_ id:10})
Suggestion: when deleting an operation, try to use _ id as a condition.
In some cases, when you perform a remove operation on a record, there may be a update operation on the record, so the record may not be deleted. If you think this is not satisfactory, you can add $atomic: db.videos.remove ({rating: {$lt: 3}, $atomic: true}) to the remove operation.
(4) Reform
Syntax db.collection.update (criteria, objNew, upsert, multi)
Criteria: the object used to set query criteria
Objnew: the object used to set the update content
Upsert: if the record already exists, update it, otherwise add a new record
Multi: if there are multiple eligible records, update them all
Note: by default, only the first eligible record is updated
If there is an update, if it does not exist, add a record db.mycollection.save (x)
Change the age whose id is 0 to 20
Db.user.update ({_ id:0}, {$set: {age:20}})
Add 20 to the age with id 0
Db.user.update ({_ id:0}, {$inc: {age:20}})
Subtract 20 from the age with an id of 0
Db.user.update ({_ id:0}, {$inc: {age:-20}})
Delete the sex field whose id is 0
Db.user.update ({_ id:0}, {$unset: {sex:1}})
Modify the hobby field whose id is 0 (provided that hobby is an array or does not exist, otherwise an error will be reported)
Db.user.update ({_ id:0}, {$push: {hobby:'football'}})
{$pushAll: {field: value_array}} uses the same as above
Add a data to the hobby field where id is 0 (provided hobby is an array or does not exist, otherwise an error will be reported)
Db.user.update ({_ id:0}, {$addToSet: {hobby:'football'}})
Delete the last data in the hobby field whose id is 0 (provided that hobby is an array or does not exist, otherwise an error will be reported
Db.user.update ({_ id:0}, {$pop: {hobby:'football'}})
Delete the eligible data in the hobby field whose id is 0 (provided that hobby is an array or does not exist, otherwise an error will be reported
Db.user.update ({_ id:0}, {$pull: {aihao:'bike'}})
Change the name of the name field in the data with id 0 to username
Db.user.update ({_ id:0}, {$rename: {'name':'username'}})
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.