Operation case dry goods collection
Modify data
db.getSiblingDB('cqust'); var query criteria ={'sno':2019000000}; var
update criteria ={$set:{'body.weight':100,'name':' Wang Xiaowang '}}; db.students.update( query criteria , update criteria );
How to modify data : First, we need to know where the data we need to modify is , Or in which field , We use $set:{ field : New value }, In fact, for this, we obviously modify the array field .
Other operations
$type Operators are based on BSON Type to retrieve the matching data types in the collection , And return the result .
Using the type of field to query :$type
First, we add the field name we need to query in the query criteria , And then there's one :{$type:'string'}, You can also use numbers :2
MongoDB The types that can be used in are shown in the following table
Type number remarks
Double1
String2
Object3
Array4
Binary data5
Undefined6 obsolete .
Object id7
Boolean8
Date9
Null10
Regular Expression11
JavaScript13
Symbol14
JavaScript (with scope)15
32-bit integer16
Timestamp17
64-bit integer18
Min key255Query with -1.
Max key127 db.getSiblingDB('cqust'); var
query criteria ={'body.weight':{$type:'string'}}; //2 it's fine too var
query criteria ={'body.weight':{$type:'double'}}; //1 it's fine too var Return condition ={};
db.students.find( query criteria , Return condition )
In this way, the query data will be the data type data we need
utilize math.random Generating random numbers , Finally, complete our update operation
// modify 2019000000 My height is 160-170 Random numbers within // modify 2019000000 The height property of is changed to string type var
query criteria ={'sno':2019000000}; var
update operation ={$set:{'body.height':Math.floor(Math.random()*10+160)}}
db.students.update( query criteria , update operation ); // Verify query var query criteria ={'sno':2019000000}; var
Return condition ={}; var result=db.students.findOne( query criteria , Return condition );
Update operation with array , Take out our data , And convert it to string type data , And then use it $set: Update operation
// Method 1 : Update specified fields ( Key points ) result.body.height=String(result.body.height) var
update operation ={$set:{'body.height':result.body.height}} db.students.update( query criteria , update operation );
Review of atomic operation knowledge
$set
A key is used to update a key value , If the key does not exist, create it .
{ $set : { field : value } }
$unset
Used to delete a key .
{ $unset : { field : 1} }
$inc
$inc A value of the document can be numeric ( Only numbers that meet the requirements ) Key to increase or decrease the operation .
{ $inc : { field : value } }
$push
hold value Append to field Go inside ,field It must be an array type , If field non-existent , An array type will be added .
usage :
{ $push : { field : value } }
$pushAll
with $push, It's just that you can append multiple values to an array field at a time .
{ $pushAll : { field : value_array } }
$pull
From array field Delete one equal to value value .
{ $pull : { field : _value } }
$addToSet
Add a value to the array , And it only increases if the value is not in the array .
$pop
Delete the first or last element of an array
{ $pop : { field : 1 } }
$rename
Modify field name
{ $rename : { old_field_name : new_field_name } }
$bit
Bit operation ,integer type
{$bit : { field : {and : 5}}}
Offset operator
> t.find() { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
"comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" : 7 } ]
} > t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true
) > t.find() { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
"comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" : 7 } ] }
//$exists Operator , Determine whether the field exists db.test.insert({"name":' Wang Xiaowang '})
db.test.insert({"name":' Wang Xiaowang -123','gender':' male ','age':19})
$exists:0(1) You can find out the existence of the previous fields , If so, the property is 1, If it doesn't exist, then the property is 0
// yes name field db.test.find({'name':{$exists:1}}) // nothing name field
db.test.find({'name':{$exists:0}})
$size: You can query whether the information in this field is empty
db.test.insert({myname:[1,2,3]}) // query myname Non zero information , And there's something wrong with it
db.test.find({myname:{$not:{$size:0},$exists:1}}) var cursor
=db.students.find() for(var i=0;i<10;i++) { if(cursor.hasNext())
printjson(cursor.next()) }
Print out the information we want , Sometimes our query returns a message , But we want to learn from you Python Print like that , Formatted output , This is more conducive to our understanding and intuitive view of the characteristics of the data
Grammar is similar to JavaScript The grammar of , and Python It's not the same , But logical thinking douchabduo
var query criteria = {'grade':2019,'class':1,'major':' big data '}; var cursor =
db.students.find( query criteria ); for(var i=0; i<cursor.length(); i++) { if
(cursor[i].gender==0)
print(" full name :"+cursor[i].name+"\t height :"+cursor[i].body.height+"\t Gender : female "); };
This is the data query case of this issue , When we learned a lot of query syntax, we found that and MySQL There is a similar phenomenon , If you want to write code according to the actual scene, you must have enough grammar and proficiency !
Every word
There is no reason to like it , Because happiness is the best reason
Technology