In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Basic operation addition, deletion, modification and search
Add: insert
Introduction: mongodb stores documents. A document is an object in json format.
Syntax: db.collectionName.isnert (document)
1: add a single document
Db.collectionName.insert ({title:'nice day'})
2: add a single document and specify _ id
Db.collectionName.insert ({_ id:8,age:78,name:'lisi'})
3. Add multiple documents
Db.collectionName.insert (
[
{time:'friday',study:'mongodb'}
{_ id:9,gender:'male',name:'QQ'}
]
)
Delete: remove
Syntax: db.collection.remove (query expressions, options)
Option refers to {justOne:true/false}, whether to delete only one line. Default is false.
Be careful
1: the query expression is still a json object
2: rows matched by query expressions will be deleted.
3: if you do not write query expressions, all documents in collections will be deleted.
Example 1: db.stu.remove ({sn:'001'})
Delete a document with a sn attribute value of '001' in the stu table
Example 2: db.stu.remove ({gender:'m',true})
Delete the document whose gender attribute is m in the stu table, deleting only 1 row.
Change update operation
Change who?-query expression
Change it to what?-- A new value or assignment expression
Operation options-optional parameters
Syntax: db.collection.update (query expressions, new values, options)
Example:
Db.news.update ({name:'QQ'}, {name:'MSN'})
It means to select the document in the news table whose name value is QQ and change its document value to {name:'MSN'}
Result: other columns in the document are also missing, with only _ id and name columns left.
That is, the new document directly replaces the old document instead of modifying it.
If you want to modify a column of the document, you can use the $set keyword
Db.collectionName.update (query, {$set: {name:'QQ'}})
Example:
If you don't want the country field, delete it using the $unset operator:
Assignment expression when modified
$set modifies the value of a column
$unset deletes a column
$rename renames a column
$inc increases a column
SetOnInsert fields that can be added when upsert is true and an insert operation occurs.
Example:
The role of Option:
{upsert:true/false,multi:true/false}
Upsert--- means that if there is no match, the row is inserted directly. (same as replace in mysql)
Example: db.stu.update ({name:'wuyong'}, {$set: {name:'junshiwuyong'}}, {upsert:true})
If there is a name='wuyong' document, it will be modified
If not, this new document will be added
Example:
Db.news.update ({_ id:99}, {upsert:true 123)
No _ id=99 document has been modified, so insert it directly
Multi: means to modify multiple lines (even if the query expression hits multiple lines, only one line will be changed by default. If you want to change multiple lines, you can use this option)
Example:
Db.news.update ({age:21}, {$set: {age:22}}, {multi:true})
Then modify all the age=21 documents in news.
Example:
Check: find, findOne
Syntax: db.collection.find (query expression, query column)
Db.collections.find (expression, {column 1:1, column 2:1})
Example 1:db.stu.find ()
Query all documents, all contents
Example 2: db.stu.find ({}, {gendre:1})
Query the gender property of all documents (the _ id attribute is always checked out by default)
Example:
Example 3: db.stu.find ({}, {gender:1, _ id:0})
Query the gender property of all documents, but not the _ id attribute
Example:
Example 3: db.stu.find ({gender:'male'}, {name:1,_id:0})
Query the name attribute in all documents whose gender attribute value is male
Example:
Operators available when Update
Example:
-> db.user.insert ({name:'lisi',age:12,sex:'male',height:123,area:'haidian'})
-> db.user.update ({name:'lisi'}, {$set: {area:'chaoyang'}, $unset: {height:1}, $inc: {age:1}, $rename: {sex:'gender'}})
Db.user.find ()
{"_ id": ObjectId ("51fc01c4f5de93e1f2856e33"), "age": 13, "area": "chaoyang", "gender": "male", "name": "lisi"}
$setOnInsert-> is equivalent to the default value of the column in mysql
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.