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

user6943528

When I query this

const pendingOfThisShop = await ShopProfile.findOne({ shop: req.shop.id, "shopsAffiliate.status":"pending" },{ shopsAffiliate: 1, _id: 0 }

I get an object like this

{
"shopsAffiliate": [
    {
        "status": "approved",
        "_id": "5db315a6de255a4444b0987b",
        "affiliateId": "5db31263a362ed4ed84c7ad5"
    },
    {
        "status": "pending",
        "_id": "5db315c5de255a4444b0987d",
        "affiliateId": "5db2b4713db4101e48836f0a"
    }
]}

I get both status:"approved" and status:"pending". I only want to get those objects in shopsAffiliate array which have status of "pending". What should I do?

Yousaf

I get both status:"approved" and status:"pending"

your query is returning the expected result.

shopsAffiliate is an array of nested documents. shopsAffiliate itself is part of documents inside ShopProfile collection. Your query checks for any document in ShopProfile that meets following 2 conditions

  1. shop: req.shop.id
  2. "shopsAffiliate.status":"pending"

When it finds any document that matches both above mentioned conditions, it returns the whole document. It doesn't cares whether there are other documents in shopsAffiliate array of the matching document that do not have pending status, it just needs to find atleast 1 document inside shopsAffiliate array that has pending status, as soon as it finds one, it returns the whole document.

I only want to get those objects in shopsAffiliate array which have status of "pending". What should I do?

you can use aggregation operation to get desired result

const pendingOfThisShop = await ShopProfile.aggregate([
      {
          $match: {
              shop: req.shop.id,
              "shopsAffiliate.status": "pending"
          }
      },
      {
          $unwind: "$shopsAffiliate"
      },
      {
          $match: { "shopsAffiliate.status": "pending" }
      }
]);

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

How to mock mongoose query like findOne()?

分類Dev

Mongoose - How add documents to array that has id of that documents

分類Dev

Mongoose.js findOne returning query metadata

分類Dev

Get all documents of a type in mongoose but only with 1 specific item of each documents array

分類Dev

How to get size of array embedded array mongoose?

分類Dev

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

分類Dev

All the subsets of size N which satisfy a condition in Haskell

分類Dev

Elasticsearch: How to query an array field using OR condition?

分類Dev

How to get a particular attribute from an array of array objects?

分類Dev

How to make a query using Mongoose that gets N results, but combines any documents it finds that meet certain criteria?

分類Dev

how to get a particular object value from nested json array

分類Dev

mongoose findOne() is not a function

分類Dev

how to get random element of array which is not null?

分類Dev

In Mongoose, query fields based on array

分類Dev

How can I fill a matrix with random values that need to satisfy a condition?

分類Dev

Sample pairs to satisfy a condition

分類Dev

Select columns that satisfy a condition

分類Dev

find object by id which is in array mongoose

分類Dev

TSQL How do i check array is a subset using JSON_QUERY

分類Dev

How to parse a string which is in particular format into HashMap?

分類Dev

How to know which utility installed a particular utility?

分類Dev

How to get a URL query string which has "?" inside?

分類Dev

Can we get IP address and/or machine of client accessing a particular website?

分類Dev

mongodb/mongoose findMany - find all documents with IDs listed in array

分類Dev

Querying subdocuments array with filter and return original documents with mongoose

分類Dev

Mongoose - Multer 内の FindOne()

分類Dev

C# LINQ Get List subset with difficult condition

分類Dev

How to use LINQ to query a subset of string

分類Dev

Get column names which do not match a particular ending string

Related 関連記事

  1. 1

    How to mock mongoose query like findOne()?

  2. 2

    Mongoose - How add documents to array that has id of that documents

  3. 3

    Mongoose.js findOne returning query metadata

  4. 4

    Get all documents of a type in mongoose but only with 1 specific item of each documents array

  5. 5

    How to get size of array embedded array mongoose?

  6. 6

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

  7. 7

    All the subsets of size N which satisfy a condition in Haskell

  8. 8

    Elasticsearch: How to query an array field using OR condition?

  9. 9

    How to get a particular attribute from an array of array objects?

  10. 10

    How to make a query using Mongoose that gets N results, but combines any documents it finds that meet certain criteria?

  11. 11

    how to get a particular object value from nested json array

  12. 12

    mongoose findOne() is not a function

  13. 13

    how to get random element of array which is not null?

  14. 14

    In Mongoose, query fields based on array

  15. 15

    How can I fill a matrix with random values that need to satisfy a condition?

  16. 16

    Sample pairs to satisfy a condition

  17. 17

    Select columns that satisfy a condition

  18. 18

    find object by id which is in array mongoose

  19. 19

    TSQL How do i check array is a subset using JSON_QUERY

  20. 20

    How to parse a string which is in particular format into HashMap?

  21. 21

    How to know which utility installed a particular utility?

  22. 22

    How to get a URL query string which has "?" inside?

  23. 23

    Can we get IP address and/or machine of client accessing a particular website?

  24. 24

    mongodb/mongoose findMany - find all documents with IDs listed in array

  25. 25

    Querying subdocuments array with filter and return original documents with mongoose

  26. 26

    Mongoose - Multer 内の FindOne()

  27. 27

    C# LINQ Get List subset with difficult condition

  28. 28

    How to use LINQ to query a subset of string

  29. 29

    Get column names which do not match a particular ending string

ホットタグ

アーカイブ