[MongoDB] uses the subscript of array elements in MongoDB to update multi-dimensional arrays.

Source: Internet
Author: User
Tags mongodb client

Mongodb DBObject supports saving multi-dimensional arrays. When adding elements, use the "$ push" operator and "$ pull" When deleting elements ".

However, the problem arises when updating. mongodb first supports "$" to locate an element in the array, for example:

View plain
  1. > T. find ()
  2. {"_ Id": ObjectId ("4b97e62bf1d8c7152c9ccb74"), "title": "ABC ",
  3. "Comments": [{"by": "Joe", "votes": 3 },{ "by": "Jane", "votes": 7}]}
  4. > T. Update ({'comments. by': 'job'}, {$ Inc: {'comments. $. Votes ': 1 }}, false, true)
  5. > T. Find ()
  6. {"_ Id": objectid ("4b97e62bf1d8c7152c9ccb74"), "title": "ABC ",
  7. "Comments": [{"by": "Joe", "votes": 4 },{ "by": "Jane", "votes": 7}]}

However, the "$" operator only supports positioning one-dimensional arrays. When updating multi-dimensional arrays, an error message is returned, for example, the following data structure:

View plain
  1. {
  2. "_ Id": objectid ("4b97e62bf1d8c7152c9ccb74 "),
  3. "Comments": [
  4. {
  5. "By": "Joe ",
  6. "Votes": 3,
  7. "Replies ":[
  8. {"By": "jane ",
  9. "Votes": 2
  10. }]
  11. }]
  12. }

What should I do if I want to increase the number of votes in {"by": "jane"} "replies" by 1 (an error will be returned when I use "$" mongodb )?

Mongodb uses a multi-dimensional array to locate an element, so our method is:

First find ({"comments. replies. by ":" jane "}) to obtain the entire object, calculate the array subscript of the corresponding reply, and then use update ({" comments.0.replies. 0.by": "jane" },{ "$ inc", {"comments.0.replies. 0. votes ": 1}), so there is no synchronization problem.

 

Ps: in fact, the better way is to provide this subscript by the user client program, that is, the mongodb client does not need to obtain this multi-dimensional array from the server and then calculate the subscript, which is too slow. If the multi-dimensional array has been presented to the user, the subscripts can be provided by the client program, which is highly efficient.

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.