Oxymore
Oxymore

Reputation: 215

$all query returning no result

I'm trying a query of this kind on my Auction collection, the objective is to return all documents with: item == 2770 and a dump == ObjectId("5104f03a46ab64d404000000") or ObjectId("51067f4946ab644003000000") or ObjectId("5106924e46ab64c81c000000").

{
    item: 2770,
    dump: {
        $all: [
            ObjectId("5104f03a46ab64d404000000"),
            ObjectId("51067f4946ab644003000000"),
            ObjectId("5106924e46ab64c81c000000")
        ]
    }
}

This query doesn't return me anything, however this one (note that I use a dump contained in the $all operator), return me a list of document. Am I ignoring something about the $all operator behavior?

{
    item: 2770,
    dump: ObjectId("5106924e46ab64c81c000000")
}

Here is an example of document in my Auction collection

{
    _id: ObjectId("5106924e46ab64c81c000237"),
    auc: 1560105766,
    item: 2770,
    owner: "Ozrael",
    bid: 450000,
    buyout: 450000,
    quantity: 20,
    timeLeft: "VERY_LONG",
    dump: ObjectId("5106924e46ab64c81c000000"),
    faction: "horde",
    bidPricePerUnit: 22500,
    buyoutPricePerUnit: 22500
}

Upvotes: 1

Views: 46

Answers (2)

Oxymore
Oxymore

Reputation: 215

I was using the wrong operator for the query, $all is matching an entire array, as said in the documentation.

The correct operator to match documents that contain either of the specified values is $in

Upvotes: 2

Vyacheslav Sermyazhko
Vyacheslav Sermyazhko

Reputation: 359

When you're using $all, every document of your result set is supposed to have all the mentioned values. The document in your example should have the following structure to be found:

{
    _id: ObjectId("5106924e46ab64c81c000237"),
    auc: 1560105766,
    item: 2770,
    owner: "Ozrael",
    bid: 450000,
    buyout: 450000,
    quantity: 20,
    timeLeft: "VERY_LONG",
    dump: [
            ObjectId("5104f03a46ab64d404000000"),
            ObjectId("51067f4946ab644003000000"),
            ObjectId("5106924e46ab64c81c000000")
          ],
    faction: "horde",
    bidPricePerUnit: 22500,
    buyoutPricePerUnit: 22500
}

Upvotes: 0

Related Questions