Python Operation MongoDB Instruction

Source: Internet
Author: User
Tags mongodb

By default, the MONGO modification modifies only the first data that is found, and if you want to modify all the records that the query matches, you must use the multi parameter.

Description of the modified operator:

$inc Grow a field with the given value;

$set replace the given key value;

$push If the field is an array, the given value will be added to the array field, and if it does not exist, it will be added automatically if the field is non-array, the error will be reported;

$PUSHALL is similar to push, except that the parameter is an array;

$unset Delete a field

$addToSet is similar to push, except that if the value already exists, it is not added;

$pop remove the first or last value of an array field, differentiated by 1 or 1;

$pull If the field is an array, you can use this operator to remove the value that satisfies the condition in the divisor group;

$pullAll is similar to pull, except that the parameter is an array;

$rename change the name of the field;

Instance:


Achieve the final result: Users (collection)

[JavaScript]View PlainCopy
    1. {
    2. _id:objectid ("50897dbb9b96971d287202a9"),
    3. Name: "Jane",
    4. AGE:20,
    5. Likes: [ "Tennis", "Golf"],
    6. Registered: false,
    7. Addr: {
    8. City: "Lyon",
    9. Country: "France"
    10. }
    11. }


In order to practice the different commands of update, first insert the basic

[JavaScript]View PlainCopy
    1. Db.users.insert ({name:"Jane", age:10})


Db.users.update (Query,obj,upsert,multi)

The parameters are query criteria {name: "Jane"}; obj changing the object; Upsert Update or Insert (Boolean); multi (if multiple lines are updated)

Enter db.users.update in the command to see the implementation logic of the update function directly

The actual upsert can be object {upsert:true,multi:true}

1, $inc for a field to increase the specified value value=old+inc; Action fields are not allowed as numbers

[JavaScript]View PlainCopy
    1. Db.users.update ({name:"Jane"},{$inc: {age:10}})

2, $set replace the original field value

[JavaScript]View PlainCopy
    1. Db.users.update ({name:"Jane"},{$set: {age:20}})

3, $push, $pushAll Add data to the field array

[JavaScript]View PlainCopy
    1. Db.users.update ({name:"Jane"},{$pushAll: {like:["Golf"}})



The rest of the instructions are a reason, and I'm not going to write anymore.

Finally, there is a problem, that is, addr field, if I only add a city: "Lyon", then County if the update it?

Similarly, the addr field is missing a county: "France", how do I add it?

Let me check the data and fix it.

Python Operation MongoDB Instruction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.