(4) mongodb operation with respect to the column

1, modify the column name

  db.person.update ({age: 31}, {$ rename: {address: 'address2'}}); age name column address column 31 is modified to address2, only modifies a record.

  db.person.update ({name: 'John Doe'}, {$ rename: {address: 'address2'}}, {multi: true}); name for the name of the column address to modify the seating is address2, modifies All records meet the conditions.

2, delete a column

  db.person.update ({name: 'John Doe'}, {$ unset: {age: ''}}); the value of the delete column name is named Joe Smith column, column names and values ​​are deleted, only delete a.

  db.person.update ({name: 'John Doe'}, {$ unset: {age: ''}}, {multi: true}); Delete the value of the name column is named column seating, column names and value will be removed to meet the conditions will be deleted.

3, a column to increase from $ inc, _id record 1, age 1 increases, may increase the decimal numeric column must be output.

  db.person.update({_id:1},{$inc:{age:1}});   

  db.person.update ({name: 'John Doe'}, {$ inc: {age: 10}}); changing a recording

  db.person.update ({name: 'John Doe'}, {$ inc: {age: 10}}, {multi: ture}); change all records satisfy the condition

4, if no query is not updated to directly insert upsert

  db.person.update ({_ id: 4}, {name: 'John Doe', class: 'three shifts', score: 90}, {upsert: true}); If no record _id 4, insert

  { "_Id": 4, "name": "John Doe", "class": "three classes", "score": 90} which records

5. If you do not update row, insert additional columns: $ setOnInsert, upsert: true

  db.person.update ({_ id: 5}, {$ setOnInsert: {name: 'Wang Wu', like: 'football'}}, {upsert: true}); after execution, the extra database recorded as follows:

  { "_Id": 5, "like": "football", "name": "Wang Wu"}, if there is a record in the database _id 5, the following statement will have no effect, not new increase, it will not be modified.

  db.person.update({_id:5},{$setOnInsert:{name:'王五',like:'football',height:178}},{upsert:true});

6, the query display fixed column, attributes: 1 shows properties: does not show 0, 0 and 1 can not mix (depending on the version)

  Original record: { "_id": 4, "name": "John Doe", "class": "three classes", "score": 90}

  Execution: db.person.find ({name: 'John Doe'}, {_ id: 1, name: 1}); result: { "_id": 4, "name": "John Doe"}

  Execution: db.person.find ({name: 'John Doe'}, {_ id: 1, name: 1, score: 1}); result: { "_id": 4, "name": "John Doe", "score": 90}

  Execution: db.person.find ({name: 'John Doe'}, {score: 0}); result: { "_id": 4, "name": "John Doe", "class": "three shifts" }

Guess you like

Origin www.cnblogs.com/javasl/p/11267417.html