1. insert operation:
Every line of mongodb (document, one of the three elements of mongodb mentioned in the previous article) is stored in the way of K-V, familiar with json shoes mongodb is certainly hand-to-hand, for Value, it may be a string, an array, or an embedded json object. The same rules also apply to BSON.
There are two common insert operations: single insert and batch insert:
1. Single insert:
var single={"name":"chenglong","password":"12345","age":20,"address":{"province":"anhui","city":"hefei"},"favourite":["apple","banana"]}db.user.insert(single)db.user.find()
You can directly declare the object, isn't it a pretty diao!
2. Batch insert: the only difference between the batch insert method and the single insert method is that you can write a for loop and put the insert statement in it. That's simple.
Ii. find operation
During normal development, we often use the following query operations:
1. >>=<=! =
2. and or in notin
Fortunately, these operations have been encapsulated in mongodb, and all developers like this. They are used as follows:
<1> "$ gt", "$ gte", "$ lt", "$ lte", "$ ne", "no special keywords ", these are one-to-one correspondence with the above 1, the code
>db.user.find({"age":{$gt:19}})>=db.user.find({"age":{$gte:19}})<db.user.find({"age":{$lt:22}})<=db.user.find({"age":{$lte:22}})!=db.user.find({"age":{$ne:22}})
<2> "No keyword", "$ or", "$ in", "$ nin" code:
And no keyword db. user. find ({"name": "chenglong", "age": 20}) ordb. user. find ({$ or: [{"address. province ":" anhui "}, {" address. province ":" hunan "}]}) indb. user. find ({"address. province ": {$ in: [" anhui "," guangdong "]}) nindb. user. find ({"address. province ": {$ nin: [" anhui "," guangdong "]})
<3> In addition, mongodb can match regular expressions, which is really powerful!
Regular Expression/* find name startwith 'C' and endwith 'G' */db. user. find ({"name":/^ c/, "name":/g $ /})
<4> when the query is very complex, we can use $ where as our big trick, because the value in where can be placed in the method body in our js!
Let's find 'chenglong'
$where/* find name="chenglong" */db.user.find({$where:function(){return this.name=='chenglong'}})
Iii. update operations
The update operation is nothing more than two types, overall update and partial update!
<1> in the previous article, the update we used is actually an overall update.
Overall Update var upd = db. user. findOne ({"name": "chenglong"}) upd. age = 30db. user. update ({"name": "chenglong"}, upd) db. user. find ()
<2>, partial update
Sometimes we only need to update one field. mongodb provides us with two modifiers: $ inc and $ set.
$ Inc Modifier
1. $ inc, short for increase, should be familiar to those who have learned SQL server. For example, if we make an online user status record, each modification will be based on the original
The value specified by auto-increment $ inc. If this key is not found in the document, a key is created. The following example shows how to use this key.
db.user.update({"name":"chenglong"},{$inc:{"age":30}})db.user.find()
2. $ set Modifier
$ Set modifier db. user. update ({"name": "chenglong" },{$ set: {"age": 10 }}) db. user. find ()
<3> upsert operation
This is the "word" created by mongodb. This upsert operation means: If I find this data stream, update it. If I do not find it, I will add a new one in the database. In fact, this is also advantageous, that is, it is easy to use to determine whether to perform the update or add operation in the database. Set the third parameter of update to true.
Upsert db. user. update ({"name": "chenglong" },{ $ inc: {"age": 11 }}, true)