0

I have database like below.

    { 
        "_id" : ObjectId("628bed8298b81711c01b6ab1"), 
        "status" : true, 
        "city" : "Kefar Sava", 
        "request" : [
            {
                "deadline" : {
                    "from" : ISODate("2022-05-03T00:00:00.000+0000"), 
                    "to" : ISODate("2022-05-31T00:00:00.000+0000")
                }, 
                "ownerId" : "628c424431884d1c140f56bc", 
                "type" : "Developer",
                "name" : "Apollo",
                "status" : "posted", 
                "_id" : ObjectId("628d496d38360a30a4c39ebd"), 
            }, 
            {
                "deadline" : {
                    "from" : ISODate("2022-05-03T00:00:00.000+0000"), 
                    "to" : ISODate("2022-06-02T00:00:00.000+0000")
                }, 
                "ownerId" : "628c424431884d1c140f56bb", 
                "type" : "Developer",
                "name" : "Apollo",
                 "status" : "posted", 
                "_id" : ObjectId("628d4a00a4ec7e12a02e74a3"), 
             }, 
        ], 
        "date" : ISODate("2022-05-23T20:24:34.052+0000"), 
        "__v" : NumberInt(0), 
    }

I want to find by ownerId, type, city and set status to canceled. I use

    const owner = await User.findOneAndUpdate(
                    {$and: [{city: city, "request.ownerId": ownerId, "request.type": type }]},
                    {$set: {"request.$.status": "canceled"}},
                    {new: true})

But I can't find by type. When I change order of that(ownerId and type), then I can't find by ownerId. Please tell me what's wrong with my code.

ray
  • 11,310
  • 7
  • 18
  • 42
Alchemist
  • 325
  • 1
  • 17
  • Does this answer your question? [How to search in array of object in mongodb](https://stackoverflow.com/questions/14040562/how-to-search-in-array-of-object-in-mongodb) – ray May 25 '22 at 20:06
  • 1
    Your query is working for me... are you sure the error is that cannot find by type? – Derek Menénedez May 25 '22 at 20:23
  • Double check your query values. – maelswarm May 25 '22 at 21:23
  • Yes. I check my code several times but the same error happens. What is possible error to that? – Alchemist May 25 '22 at 21:49
  • 1
    Could you check finding by `city`, `type` and `ownerId` please? I think it couldn't find by third expression. Since `ownerId` is unique in that `db` and it is second expression, it may return desired value. But if you change the order of that, it will return first request always. – Alchemist May 25 '22 at 21:59

0 Answers0