MongoDB How to update the first item that matches a condition in an array of documents?

delsin

I have a sample document:

{
    _id: 1,
    exams: [
               {name: 'a', score: 2},
               {name: 'b', score: 9},
               {name: 'c', score: 7}
           ]
}

I need to set the score of the first element that has a score higher than 7 in the exams array to 10, for a document specified by _id.

So in the case of the sample document, I need to update it to:

{
    _id: 1,
    exams: [
               {name: 'a', score: 2},
               {name: 'b', score: 10},
               {name: 'c', score: 7}
           ]
}

What would the Mongo shell query be for this task?

Manohar Gavit

Use $gt to find higher score

updateOne({"exams.score":{ $gt: 7 }},{$set:{"exams.$.score":"10"})

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to update item from array nested within array

分類Dev

How to create MongoDB Update query to update certain elements in an array of objects

分類Dev

How to get the first item from an associative PHP array?

分類Dev

How to query findOne() in mongoose such that we get subset of array of documents which satisfy particular condition?

分類Dev

How to find null documents in mongodb?

分類Dev

Mongodb update array of objects

分類Dev

MongoDB aggregate - average on specific values in array of documents

分類Dev

Update an array item in knockout js

分類Dev

MongoDB - Find documents matching certain condition for unknown field keys

分類Dev

How to convert ISO Date to 'yyyy-mm-dd hh:mm:ss' in mongodb from embedded array of documents?

分類Dev

MongoDB. How to update json property inside an array

分類Dev

How to update array elements in MongoDB instead of creating new one

分類Dev

How to Update Multiple Fields Inside an Array in Mongodb, Express, NodeJs?

分類Dev

Creation of array of arrays fails, when first size of first dimension matches

分類Dev

How to find all documents where the value of one field matches that of another

分類Dev

react useState add and remove item to array with condition

分類Dev

Extract $graphLookup matches into documents

分類Dev

Mongo aggregation: Count documents based on array field matching condition

分類Dev

How to update a subdocument in mongodb

分類Dev

How to stop insertion of Duplicate documents in a mongodb collection

分類Dev

How to get keys of nested mongodb documents

分類Dev

MongoDB - how to merge dict field of documents?

分類Dev

How to get matched sub documents from mongodb?

分類Dev

Filtering out documents with a specific item in an array field using mongoid

分類Dev

Get the first and last item in an Array - JS

分類Dev

My loop is only checking the first array item

分類Dev

Flattening an array so first item is key

分類Dev

Different rendering of first item from rest of array

分類Dev

Mongodb unset from array under condition

Related 関連記事

  1. 1

    How to update item from array nested within array

  2. 2

    How to create MongoDB Update query to update certain elements in an array of objects

  3. 3

    How to get the first item from an associative PHP array?

  4. 4

    How to query findOne() in mongoose such that we get subset of array of documents which satisfy particular condition?

  5. 5

    How to find null documents in mongodb?

  6. 6

    Mongodb update array of objects

  7. 7

    MongoDB aggregate - average on specific values in array of documents

  8. 8

    Update an array item in knockout js

  9. 9

    MongoDB - Find documents matching certain condition for unknown field keys

  10. 10

    How to convert ISO Date to 'yyyy-mm-dd hh:mm:ss' in mongodb from embedded array of documents?

  11. 11

    MongoDB. How to update json property inside an array

  12. 12

    How to update array elements in MongoDB instead of creating new one

  13. 13

    How to Update Multiple Fields Inside an Array in Mongodb, Express, NodeJs?

  14. 14

    Creation of array of arrays fails, when first size of first dimension matches

  15. 15

    How to find all documents where the value of one field matches that of another

  16. 16

    react useState add and remove item to array with condition

  17. 17

    Extract $graphLookup matches into documents

  18. 18

    Mongo aggregation: Count documents based on array field matching condition

  19. 19

    How to update a subdocument in mongodb

  20. 20

    How to stop insertion of Duplicate documents in a mongodb collection

  21. 21

    How to get keys of nested mongodb documents

  22. 22

    MongoDB - how to merge dict field of documents?

  23. 23

    How to get matched sub documents from mongodb?

  24. 24

    Filtering out documents with a specific item in an array field using mongoid

  25. 25

    Get the first and last item in an Array - JS

  26. 26

    My loop is only checking the first array item

  27. 27

    Flattening an array so first item is key

  28. 28

    Different rendering of first item from rest of array

  29. 29

    Mongodb unset from array under condition

ホットタグ

アーカイブ