In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Database MongoDB document operation is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Document operation of MongoDB
In MongoDB, a document means that multiple keys and their associated values are placed together in an orderly manner. In fact, it refers to data, and it is also the part that we usually operate the most.
The data structure of documents in MongoDB is basically the same as that of JSON. All data stored in the collection is in BSON format.
BSON is a binary storage format similar to JSON, which is short for Binary JSON.
Insert a document insert a single document
Note:
You do not need to consider field when you add it. If the field already exists, add it to the specified field. If field does not exist, add a new filed to collection
When adding data to collection, if the collection does not exist, the collection is automatically created
Inserts a single document into the dev collection.
You can use insert/insertOne/save to perform the addition, and the syntax is exactly the same. The following three writing methods are equivalent:
Db.c1.insert ({name: "Zhang San"}); db.c1.save ({name: "Zhang San"}); db.c1.insertOne ({name: "Zhang San"})
Difference:
When a primary key is explicitly given, save indicates a modification if the primary key value already exists, insert/insertOne will report a duplicate primary key.
Note: (save modification must be performed on the client side of MongoDB, and third-party clients cannot be used, otherwise it cannot be performed successfully)
In the command, _ id is of type ObjectId, and the string needs to be converted to ObjectId through the ObjectId function.
Db.c1.save ({_ id:ObjectId ("5e81b3ac4d4d000026004f6a"), name: "jqk"}) inserts multiple documents
You can use insert/insertMany/save to perform the addition, which is different from the single addition to change the new function parameter from object type ({}) to array type ([{}]). The following three words are equivalent:
Db.c1.insert ([{name: "a"}, {name: "b"}]); db.c1.insertMany ([{name: "a"}, {name: "b"}]); db.c1.save ([{name: "a"}, {name: "b"}]); update the document
MongoDB updates the documents in the collection through the update function or the save function.
Update function
The update () function is used to update existing documents.
Syntax format: db.COLLECTION_NAME.update ({query criteria}, {update content}, {update parameters (optional))
The update content updates the content for the entire document. If there is only one property in the update content, all properties except _ id will be set to null.
Add a new test data first
Db.c1.insert ({name: "Zhang San", age:12,address: "address"})
Modified, name has been changed to Li Si, age and address have been set to null. (in mongodb, a property is deleted if all document objects in a collection are empty.)
Db.c1.update ({name: "Zhang San"}, {name: "Li Si"}); update operator $set operator (need to remember)
$set operator: used to specify a key and update the key value, if the key does not exist and create. Only the first document can be modified
Syntax format: db.COLLECTION_NAME.update ({query criteria}, {update operator: {update content}})
Summary of the role of $set:
Modify only a specific Field to solve the problem that update modifies the entire document by default
Db.c1.update ({name: "Zhang San"}, {$set: {name: "Wang Wu"}})
By default, only the first document that meets the criteria is modified. If all modifications are needed, the update parameter multi:true is added.
Db.c1.update ({name: "Zhang San"}, {$set: {age:18}}, {multi:true})
If Field does not exist, you can create a new Field
Db.c1.update ({name: "Zhang San"}, {$set: {sex: "male"}}) $inc operator
$inc operator: you can add or subtract a key whose value is numeric (only a number that meets the requirements) in a document. If a positive number is given as an addition, and a negative number is given as a decrease.
Reduce the age of Wang Wu by 5 years.
Db.c1.update ({name: "Wang Wu"}, {$inc: {age:-5}}); $unset operator
$unset operator: mainly used to delete keys. Leave the value of the key empty. When writing the command, the value of field in $unset is arbitrary, and no matter what value is given, it indicates deletion.
Delete the address named Wang Wu.
Db.c1.update ({name: "Wang Wu"}, {$unset: {address: "random"}}); $push operator
$push operator: adds an array element to a key of an array type in the document without filtering duplicate data. When a key is added, the key value type must be an array; if the key does not exist, an array type key is created.
The array type attribute hobby is added to all document objects in collection C1 and a value is added to write the code. If executed again, it means adding another value to the hobby to write code. By default, only the first item that meets the criteria is modified. If you need to modify all of them, add the multi attribute.
Db.c1.update ({}, {$push: {hobby: "write code"}}, {multi:true}); $pop operator
$pop operator: deletes data elements. Available values can only be 1 or-1. 1 means tail deletion,-1 means head deletion.
Delete the first element in the hobby. Where key in $pop is the array type attribute to operate on.
Db.c1.update ({name: "Li Si"}, {$pop: {hobby:-1}}) $pull operator
$pull operator: removes elements that meet the criteria from the array, as long as the conditions are met.
Delete the element content in hobby to watch school videos. If there are multiple elements, delete them.
Db.c1.update ({name: "Wang Wu"}, {$pull: {hobby: "watching Shang School Video"}})
$pullAll operator
$pullAll operator: multiple conditions can be set.
Delete the value of hobby in Wang Wuzhong to write code and watch school videos. The value of the hobby must be an array type.
Db.c1.update ({name: "Wang Wu"}, {$pullAll: {hobby: ["write code", "watch the video of Shang School"]}}); $rename
$rename operator: rename the key. Any type of key can be renamed.
Change the name property of Wang Wu to username.
Db.c1.update ({name: "Wang Wu"}, {$rename: {name: "username"}}); query document find () function
In MongoDB, you can use the find () function to query documents.
Syntax format: find ({query criteria (optional)}, {specify projected key (optional)})
If no parameter is given, all data is queried.
Db.c1.find ()
Query all document objects whose name is Zhang San
Db.c1.find ({name: "Zhang San"}) projection operation
A projection query refers to which columns are displayed or not displayed. Attributes written to projection can be 1 (displayed) or 0 (not displayed). The values of all properties except _ id must be the same.
Sex and hobby are not displayed, other properties are displayed
Db.c1.find ({name: "Zhang San"}, {sex:0,hobby:0})
Displays sex and hobby, and the default _ id is also displayed
Db.c1.find ({name: "Zhang San"}, {sex:1,hobby:1})
Only sex and hobby are displayed. Only in this case can there be different values of attributes.
Db.c1.find ({name: "Zhang San"}, {_ id:0,sex:1,hobby:1}) findOne () function
The findOne () function returns only the first piece of data that meets the condition.
Returns the first row of document objects.
Db.c1.findOne ()
Returns the first piece of data named Zhang San
Db.c1.findOne ({name: "Zhang San"}) regular query
Query conditions in MongoDB can also use regular expressions as matching constraints. Regular expression syntax is exactly the same as JavaScript regular expression statements. Regular content needs to be written before / /.
Syntax format: db.COLLECTION_NAME.find ({field name: regular expression});,
Fuzzy query can be realized.
Query document objects with three existing in name
Db.c1.find ({name:/ 3 /})
Or
Db.COLLECTION_NAME.find ({field name: {$regex: regular expression, $options: regular option}})
The second method has more regular options and more functions than the first one.
Regular expression format: / xxx/
Regular options:
I-case-insensitive to match case.
M-Multiline lookup, if there is no newline symbol in the content (for example,\ n) or conditionally no (start/end), this option has no effect
X-when the x option is set, non-escaped white space characters in regular expressions are ignored. Regex and options syntax are required
S-allows dot characters (I.) to match all characters, including newline characters. Regex and options syntax are required
IMagne m _ m _ x _ r s can be combined.
Example:
Input data
Db.c1.insert ({name: "abc"}); db.c1.insert ({name: "bcd"}); db.c1.insert ({name: "ABC"}); db.c1.insert ({name: "BCD"})
You can only query document objects that contain lowercase b
Db.c1.find ({name:/b/})
When querying, it is case-insensitive and can be queried as long as it contains b or B.
Db.c1.find ({name: {regex:/b/, regex:/b/, regex:/b/,options: "I"}})
Conditional operator
The conditional operator is used to compare two expressions and get data from the mongoDB collection.
Syntax format: find ({key: {operator: condition}}) or findOne ({key: {operator: condition}})
$gt
(>) is greater than the operator, greater than abbreviation. Use right memory to indicate the right corner bracket.
Query all document objects older than 10
Db.c1.find ({age: {$gt:10}}); $lt
(=) greater than or equal to the operator greater than equals
Query all document objects that are 8 or older. Cannot be queried if the document does not have an age attribute.
Db.c1.find ({age: {$gte:8}}); $lte
(
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.