liegolas
liegolas

Reputation: 53

MongoDB: aggregation $lookup between ObjectIds returning an empty array

I'm trying to perform an aggregation using $lookup between two fields of type ObjectId. I've already checked my data and it should be returning something. Also, the schemas indeed define the fields as ObjectId, so if I am not mistaken I should not need to convert the data type as I've seen in some posts. Here's my query:

db.workerlocationcontexts.aggregate([ 
    {
        $lookup: {
            from: "LocationSensors",
            localField: "sensor",
            foreignField: "_id",
            as: "test"
        }
    }
])

Here are my collections/sample data:

The result I'm getting are all my WorkerLocationContexts, but the field "test" returns as an empty array.

Can someone help me?

Thanks in advance!

Upvotes: 1

Views: 875

Answers (1)

J.F.
J.F.

Reputation: 15235

from field into $lookup seems to be case sensitive so it is a common problem the collection name typed in that field.

One common problem is not add the final "s" while using mongoose. Os, as in this case, the use of capital letters, yo using from: locationsensors should works like this example

The from field has to be the same as the collection name, otherwise Mongo can't find the desired collection.

Upvotes: 4

Related Questions