MongoDB Update character manipulation

Source: Internet
Author: User
Tags findone mongodb update vars

MongoDB Update character manipulation

Zhang Ying published in 2014-07-23

Category list: NoSQL

Tags: $inc, $rename, $set, $unset, MongoDB, Multi, update, Upsert

The following commonly used update operation, with the mongodb2.6.3 version of the test, the official release of stable version 2.4, the proposed stable version.

One, upsert means that if there is data is not inserted, no data inserted

1, under command line

View copy print?
  1. > db.peoples.update ( //Find users with name equals tank
  2. ... {name: "Tank"},
  3. ... {
  4. ... "_id": 1,
  5. ... name: "Andy",
  6. ... rating:10,
  7. ... score:10
  8. ... },
  9. ...      {Upsert:true} //If not, insert
  10. ... );
  11. Writeresult ({ "nmatched": 0, "nupserted": 1, "_id": 1})
  12. > Db.peoples.find ();
  13. { "_id": 1, "name": "Andy", "rating": Ten, "Score": Ten}
  14. > Db.peoples.update (
  15. ... {_id:1},
  16. ... {
  17. ... "_id": 1,
  18. ... name: "Andy",
  19. ... rating:10,
  20. ... score:10
  21. ... },
  22. ... {Upsert:true}
  23. ... );
  24. Writeresult ({ "nmatched": 1, "nupserted": 0}) //No insert operation with matching data
  25. > Db.peoples.find ();
  26. { "_id": 1, "name": "Andy", "rating": Ten, "Score": Ten}

2,php Upsert operation

View copy print?
    1. $collection->update (
    2. Array ("name" = " Zhang"),
    3. Array ("_id" =>2,"name" = "Tank","rating" =>10,"Score" =>10),
    4. Array ("upsert" = True)
    5. );
    6. Print_r ($collection->findone ());

Second, $set replacement value

1, command-line operation

View copy print?
  1. > Db.peoples.find ();
  2. { "_id": 1, "name": "Andy", "rating": Ten, "Score": Ten}
  3. > Db.peoples.update (
  4. ... {_id:1},
  5. ... {
  6. ... $set: {rating:18}
  7. ... }
  8. ... );
  9. Writeresult ({ "nmatched": 1, "nupserted": 0})
  10. > Db.peoples.find ();
  11. { "_id": 1, "name": "Andy", "rating":, "Score": Ten}

2,php $set operation

View copy print?
    1. $where = Array ("_id" =>1);
    2. $param = Array (' $set ' = = =Array ("score" = "100")); //Note that the set here must be a single quote
    3. $collection->update ($where,$param);
    4. Print_r ($collection->findone ());

Third, $inc if there is no direct assignment to the response paragraph, if there is a value added to the original value

1, command-line operation

View copy print?
  1. > Db.peoples.find ();
  2. { "_id": 1, "name": "Andy", "rating": +, "score": Ten}
  3. > Db.peoples.update (
  4. ... {_id:1},
  5. ... {
  6. ... $inc: {age:30}
  7. ... }
  8. ... );
  9. Writeresult ({ "nmatched": 1, "nupserted": 0})
  10. > Db.peoples.find ();
  11. { "_id": 1, "age": +, "name": "Andy", "rating": " Score": ten} //First time, add a field
  12. > Db.peoples.update (
  13. ... {_id:1},
  14. ... {
  15. ... $inc: {age:30}
  16. ... }
  17. ... );
  18. Writeresult ({ "nmatched": 1, "nupserted": 0})
  19. > Db.peoples.find ();
  20. { "_id": 1, "age": $, " name": "Andy", "rating": " Score": ten} //The second time, found this field to add the value of the

2,php $inc operation

View copy print?
    1. $where = Array ("_id" =>1);
    2. $param = Array (' $inc ' = =Array ("age" =>30));
    3. $collection->update ($where,$param);
    4. Print_r ($collection->findone ());

Four, $unset delete fields

1, command-line operation

View copy print?
  1. > Db.peoples.find ();
  2. { "_id": 1, "age": +, "name": "Andy", "rating":, "Score": Ten}
  3. > Db.peoples.update (
  4. ... {_id:1},
  5. ... {
  6. ... $unset: {Age: "} //delete age field
  7. ... }
  8. ... );
  9. Writeresult ({ "nmatched": 1, "nupserted": 0})
  10. > Db.peoples.find ();
  11. { "_id": 1, "name": "Andy", "rating": +, "score": Ten}
  12. >

2,php $unset operation

View copy print?
    1. $where = Array ("_id" =>1);
    2. $param = Array (' $unset ' = = =Array ("score" and "="));
    3. $collection->update ($where,$param);
    4. Print_r ($collection->findone ());

Five, $rename modify field names

1, command-line operation

View copy print?
  1. > Db.peoples.find ();
  2. { "_id": 1, "name": "Andy", "rating":
  3. > Db.peoples.update (
  4. ... {_id:1},
  5. ... { $rename: { "name": "FirstName"}} //change name to FirstName
  6. ... );
  7. Writeresult ({ "nmatched": 1, "nupserted": 0})
  8. > Db.peoples.find ();
  9. { "_id": 1, "FirstName": "Andy", "rating":

2,php $rename operation

View copy print?
    1. $where = Array ("_id" =>1);
    2. $param = Array (' $rename ' = = =Array ("FirstName" = "name")); //change FirstName to name
    3. $collection->update ($where,$param);
    4. Print_r ($collection->findone ());

Six, multi update more than one data

1, command-line operation

View copy print?
  1. > Db.peoples.find ();
  2. { "_id": 1, "name": "Andy", "rating":
  3. { "_id": 2, "name": "Zhang", "rating": 3, "Score": 5}
  4. { "_id": 3, "name": "Hao", "rating": +, "score": 5}
  5. > Db.peoples.update (
  6. ... {rating: { $gt:}},
  7. ... { $inc: {score:10}},
  8. ... {Multi:true}
  9. ... );
  10. Writeresult ({ "nmatched": 2, "nupserted": 0})
  11. > Db.peoples.find ();
  12. { "_id": 1, "name": "Andy", "rating": " Score": ten} //This data updated
  13. { "_id": 2, "name": "Zhang", "rating": 3, "Score": 5}
  14. { "_id": 3, "name": "Hao", "rating": +, "score": [] //This data updated

2,php Multi Operation

View copy print?
    1. $where  = array ( Span class= "string" > "rating" =>array ( ' $gt ' =>10));  
    2. $param  = array ( ' $set ' =>array ( "name" =
    3. $ismore  = array ( Span class= "string", "multiple"  => true);   
    4. $collection->update ( $where, $param, $ismore)   

Version is different, the function will be different, for example, I use the version is 2.6.3, $min and $max can not use, will be reported "errmsg": "Invalid modifier specified $min", Hope MongoDB out a stable high version.

Reprint Please specify
Author: submarine Eagle
Address: http://blog.51yip.com/nosql/1638.html

MongoDB Update character manipulation

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.