How to project specific elements in an Array field in MongoDB?

Saqy G

I have this data in MongoDB:

{
"_id" : ObjectId("5c7e459f875ea5548de25722"),
"Autos" : [
    {
        "_id" : ObjectId("5cad9759e1c3895999adaceb"),
        "deleted" : 1,

    },
    {
        "_id" : ObjectId("5cad9a8be1c3895999adacef"),
        "deleted" : 0,

    },
    {
        "_id" : ObjectId("5cad9aa4e1c3895999adacf0"),
        "deleted" : 0,
    }
]
    }

{
"_id" : ObjectId("5c7e45e9875ea5548de25724"),
"Shoemaking" : [
    {
        "_id" : ObjectId("5cad9770e1c3895999adacec"),
        "deleted" : 1,
    },
    {
        "_id" : ObjectId("5cad9a5de1c3895999adaced"),
        "deleted" : 0,
    },
]

I want to basically select * from table where deleted = 0
show where the deleted records are equal to 0.

Here is what I have tried so far:

db.rental.find({"Autos.deleted":{$ne: 1}}).pretty()

db.rental.find({"Autos":  {$elemMatch: {deleted: 1 } } } ).pretty()

db.rental.find({"Autos.deleted": 0},{"Autos": {$elemMatch: 
{deleted:0}}});

But none of the above work for me. What am I doing wrong?

Desired output:

{
    "_id" : ObjectId("5c7e459f875ea5548de25722"),
    "Autos" : [
        {
            "_id" : ObjectId("5cad9a8be1c3895999adacef"),
            "deleted" : 0,

        },
        {
            "_id" : ObjectId("5cad9aa4e1c3895999adacf0"),
            "deleted" : 0,
        }
    ]
        }

        {
          "_id" : ObjectId("5c7e45e9875ea5548de25724"),
          "Shoemaking" : [
              {
                  "_id" : ObjectId("5cad9a5de1c3895999adaced"),
                  "deleted" : 0,
              },
          ]
}

I want the output to be something like the above, but all the queries I have tried so far either select only one record of Array or select nothing at all.

Sreeragh A R
db.rental.aggregate([
   {
      $project: {
         Autos: {
            $filter: {
               input: "$Autos",
               as: "auto",
               cond: { $eq:["$$auto.deleted",0] }
            }
         }
      }
   }
])

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

MongoDB: how to get specific elements from array?

分類Dev

Move field to a specific object in an array in MongoDB

分類Dev

How to compare two object elements in a mongodb array

分類Dev

How to delete particular no of elements from array in mongodb

分類Dev

How to update a specific field in Mongodb knowing it's name?

分類Dev

how to get sum of int array field in projection in spring data mongodb

分類Dev

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

分類Dev

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

分類Dev

in python how to replace specific elements of array randomly with a certain probability?

分類Dev

Exclude field in array of subdocument in mongodb

分類Dev

How to increment a field in mongodb?

分類Dev

Pick specific elements from array

分類Dev

Search/print specific elements in array

分類Dev

Array results are not displayed when I am accessing the array,How can I access specific field values in this array

分類Dev

Query on rows where the elements of a specific field are not empty

分類Dev

MongoDB Replace specific array values

分類Dev

How to add a field with specific datatype ( list, object) in an existing mongoDB document using python

分類Dev

Merge array elements on common field in JQ

分類Dev

How to update a field universally in MongoDB?

分類Dev

Update field of a document inside an array of arrays in mongodb

分類Dev

Mongodb distinct on a array field with regex query?

分類Dev

check if a field value exit in array - MongoDB

分類Dev

Push an element into an array if a field satisfies a condition, MongoDB

分類Dev

Sort an array and add a rank field in MongoDB

分類Dev

MongoDB : limit query to a field and array projection

分類Dev

Adding up values from array elements in MongoDB

分類Dev

MongoDB Aggregation Project check if array contains

分類Dev

MongoDB aggregate - average on specific values in array of documents

分類Dev

MongoDB map filtered array inside another array with aggregate $project

Related 関連記事

  1. 1

    MongoDB: how to get specific elements from array?

  2. 2

    Move field to a specific object in an array in MongoDB

  3. 3

    How to compare two object elements in a mongodb array

  4. 4

    How to delete particular no of elements from array in mongodb

  5. 5

    How to update a specific field in Mongodb knowing it's name?

  6. 6

    how to get sum of int array field in projection in spring data mongodb

  7. 7

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

  8. 8

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

  9. 9

    in python how to replace specific elements of array randomly with a certain probability?

  10. 10

    Exclude field in array of subdocument in mongodb

  11. 11

    How to increment a field in mongodb?

  12. 12

    Pick specific elements from array

  13. 13

    Search/print specific elements in array

  14. 14

    Array results are not displayed when I am accessing the array,How can I access specific field values in this array

  15. 15

    Query on rows where the elements of a specific field are not empty

  16. 16

    MongoDB Replace specific array values

  17. 17

    How to add a field with specific datatype ( list, object) in an existing mongoDB document using python

  18. 18

    Merge array elements on common field in JQ

  19. 19

    How to update a field universally in MongoDB?

  20. 20

    Update field of a document inside an array of arrays in mongodb

  21. 21

    Mongodb distinct on a array field with regex query?

  22. 22

    check if a field value exit in array - MongoDB

  23. 23

    Push an element into an array if a field satisfies a condition, MongoDB

  24. 24

    Sort an array and add a rank field in MongoDB

  25. 25

    MongoDB : limit query to a field and array projection

  26. 26

    Adding up values from array elements in MongoDB

  27. 27

    MongoDB Aggregation Project check if array contains

  28. 28

    MongoDB aggregate - average on specific values in array of documents

  29. 29

    MongoDB map filtered array inside another array with aggregate $project

ホットタグ

アーカイブ