MongoDB study notes

  • 2020-05-07 20:38:15
  • OfStack

1. Configuration: mongod --dbpath=D:\MongoDB\data

mongo

2. Basic addition, deletion, examination and reform

find() update()-- whole update, partial update.

Modifier: $inc db.person.update ({"age":23},{$inc:{"salary":1000}})

The first parameter is a condition. The second parameter is the modified value, but the value must be an integer. ($inc allowed for numbers only)

$set modifier: db.person.update ({"name":"gll"},{$set:{"age":25}}}

There is one more operation to modify or add: the insertOrUpdate operation can be called here.

Just set the third parameter of update to true. If it is not found, add a new one to the database to avoid the database from deciding whether it is an update or add operation. Easy to use.

Batch update:

If multiple matches are made, only the first is updated by default. If multiple updates are required, set true as the fourth parameter of update. It's easy.

Remove operation.

3. Advanced operation

Aggregation:

Which property count() distinct() selects cannot be repeated.

group() parameter key: specifies the basis by which documents are grouped, with all age key values grouped into 1 group and true as the value of the return key age.

initial: "initial":{"person":[]} the number of initial reduce function calls per group. All members of group 1 use this accumulator.

To put it bluntly. This is an initial value. Each time $reduce calls this value, it changes the value.

Example:


db.person.group({
. "key":{"age":true},
. "initial":{"person":[]},
. "$reduce":function(cur,prev){  The first 1 Is the current document, the first 2 For the accumulator document 
. prev.person.push(cur.name);
.   }
. })
eg : db.person.group({
"key": {
"age": true
},
"initial": {
"person": [
]
},
"$reduce": function(doc,out){
out.person.push(doc.name);
},
"finalize": function(out){
out.count=out.person.length;
},
"condition": {
"age": {
$lt: 25
}
}


Related articles: