I only want to see matching documents, i.e. only T3 in the example below. I can find the matching documents between lotterytickets (many documents) and lotterydrawing (only a few documents).
How can I filter out the non-matching documents? Basically, I'd not like to see documents with the condition drawnticket==[], but I haven't found the conditional code to apply.
Any help would be appreciated. Thank you in advance
Configuration:
db={
"lotteryticket": [
{
"_id": ObjectId("6021ce0cb4d2c2b4f24c3a2e"),
"ticket": "T1",
"player": "Alice"
},
{
"_id": ObjectId("6021ce0cb4d2c2b4f24c3a2f"),
"ticket": "T2",
"player": "Bob"
},
{
"_id": ObjectId("6021ce0cb4d2c2b4f24c3a33"),
"ticket": "T3",
"player": "Charles"
}
],
"lotterydrawing": [
{
"_id": ObjectId("63309480b749b733c087b758"),
"ticket": "T3"
},
{
"_id": ObjectId("63309480b749b733c087b759"),
"ticket": "T9"
},
{
"_id": ObjectId("63309480b749b733c087b75a"),
"ticket": "T77"
}
]
}
Query:
db.lotteryticket.aggregate([
{
$lookup: {
from: "lotterydrawing",
localField: "ticket",
foreignField: "ticket",
as: "drawnticket",
}
}
])
Result:
[
{
"_id": ObjectId("6021ce0cb4d2c2b4f24c3a2e"),
"drawnticket": [],
"player": "Alice",
"ticket": "T1"
},
{
"_id": ObjectId("6021ce0cb4d2c2b4f24c3a2f"),
"drawnticket": [],
"player": "Bob",
"ticket": "T2"
},
{
"_id": ObjectId("6021ce0cb4d2c2b4f24c3a33"),
"drawnticket": [
{
"_id": ObjectId("63309480b749b733c087b758"),
"ticket": "T3"
}
],
"player": "Charles",
"ticket": "T3"
}
]
https://mongoplayground.net/p/bYcLEzrF5QT
Add a match stage, to filter stages with the empty drawn tickets. Like this:
db.lotteryticket.aggregate([
{
$lookup: {
from: "lotterydrawing",
localField: "ticket",
foreignField: "ticket",
as: "drawnticket",
}
},
{
"$match": {
$expr: {
"$gt": [
{
$size: "$drawnticket"
},
0
]
}
}
}
])
Playground.
try this query
db.lotteryticket.aggregate([
{
$lookup: {
from: "lotterydrawing",
localField: "ticket",
foreignField: "ticket",
as: "drawnticket"
}
},
{
"$match": {
drawnticket: {
$exists: true,
$ne: []
}
}
}
])
Playground.
Related
I have two collections in MongoDB: "carts" and another "products"
Carts:
[{
"_id": {
"$oid": "62af0fefebc0b42a875c7df1"
},
"uuid": "6ca05ae0-522a-4db3-b380-2d2330ee1e27",
"cartitems": [
"62a0b24680cc2891148daf7b",
"62a7339d91d01868921afa0a",
"62a72f7191d01868921afa08",
"62a7330291d01868921afa09"
],
"created": "2022-06-19T14:00:47.846958537+02:00[Europe/Brussels]",
"lastupdate": "2022-06-19T14:01:06.15165564+02:00[Europe/Brussels]"
},
{...},
{...}]
products:
[{
"_id": {
"$oid": "62a0b24680cc2891148daf7b"
},
"name": "Product1",
"created": "2022-06-11T09:41:54.461308647+02:00[Europe/Brussels]",
"category": "Workshops",
"pricein": "28900",
"lastupdate": "2022-06-17T16:09:53.385655474+02:00[Europe/Brussels]"
},
{...},
{...}]
I would like to use Aggregate:
db.carts.aggregate([
{ $match: { uuid: "6ca05ae0-522a-4db3-b380-2d2330ee1e27" } },
{
$lookup: {
from: "products",
localField: "cartitems",
foreignField: "_id",
as: "output"
}
}
])
This is not working because localField: "cartitems" should be converted:
"$addFields": {
"convertedIdStr": {
"$toString": "$cartitems"
}
But I don't manage to convert because cartitems is an array.
Any help would be great, Thanks a lot!
Use $lookup with pipeline. In $lookup pipeline stage, add $match stage by filtering the (converted to string) product's _id is in cartitems (array) variable.
db.carts.aggregate([
{
$match: {
uuid: "6ca05ae0-522a-4db3-b380-2d2330ee1e27"
}
},
{
$lookup: {
from: "products",
let: {
cartitems: "$cartitems"
},
pipeline: [
{
$match: {
$expr: {
$in: [
{
$toString: "$_id"
},
"$$cartitems"
]
}
}
}
],
as: "output"
}
}
])
Sample Mongo Playground
I have the following MongoDB structure:
Division Collection:
{
"_id": ObjectId("5b28cab902f28e18b863bd36"),
"name": "Premier League",
...
"teams": [
ObjectId("5b28cab902f28e18b863bd01"),
ObjectId("5b28cab902f28e18b863bd02"),
ObjectId("5b28cab902f28e18b863bd03"),
...
]
...
},
...
Teams Collection:
{
"_id": ObjectId("5b28cab902f28e18b863bd01"),
"name": "Liverpool",
...
"players": [
ObjectId('5b23tmb902f28e18b863bd01'),
ObjectId('5b23tmb902f28e18b863bd02'),
ObjectId('5b23tmb902f28e18b863bd03'),
...
]
...
},
...
Players Collection:
{
"_id": ObjectId("5b2b9a8bbda339352cc39ec1"),
"name": "Mohamed Salah",
"nationality": [
ObjectId("5b23cn1902f28e18b863bd01"),
ObjectId("5b23cn2902f28e18b863bd02"),
],
...
},
...
Countries Collection:
{
"_id": ObjectId("5b23cn1902f28e18b863bd01"),
"name": "England",
...
},
{
"_id": ObjectId("5b23cn2902f28e18b863bd02"),
"name": "Egypt",
...
},
...
How to get a result, which is below, using MongoDB aggregation ($lookup, $pipeline, etc):
{
"divisions": [
{
"_id": ObjectId("5b28cab902f28e18b863bd36"),
"name": "Premier League",
...
"teams": [
{
"_id": ObjectId("5b28cab902f28e18b863bd01"),
"name": "Liverpool",
...
"players": [
{
"_id": ObjectId("5b23tmb902f28e18b863bd01"),
"name": "Mohamed Salah",
"nationality": [
{
"_id": ObjectId("5b23cn2902f28e18b863bd02"),
"name": "Egypt",
...
},
{
"_id": ObjectId("5b23cn1902f28e18b863bd01"),
"name": "England",
...
}
]
...
},
...
]
},
...
]
},
{
"_id": ObjectId("5b28cab902f28e18b863bd37"),
"name": "Championship",
...
},
...
]
}
I manage to make a first-level merge:
db.divisions.aggregate([
{
$lookup: {
from: 'teams',
localField: 'teams',
foreignField: '_id',
as: 'teams'
}
},
])
and then I ran into difficulties, so I would be very grateful if someone could help me with this issue.
You need multi-level nested $lookup with pipeline.
db.division.aggregate([
{
$lookup: {
from: "teams",
let: {
teams: "$teams"
},
pipeline: [
{
$match: {
$expr: {
$in: [
"$_id",
"$$teams"
]
}
}
},
{
$lookup: {
from: "players",
let: {
players: "$players"
},
pipeline: [
{
$match: {
$expr: {
$in: [
"$_id",
"$$players"
]
}
}
},
{
$lookup: {
from: "countries",
localField: "nationality",
foreignField: "_id",
as: "nationality"
}
}
],
as: "players"
}
}
],
as: "teams"
}
}
])
Sample Mongo Playground
Maybe someone will be useful. Data also can be merged using the populate method:
db.divisions.find(_id: division_id).populate(
{
path: 'teams',
populate: {
path: 'players',
populate: {
path: 'nationality'
}
}
}
)
Here's a lookup query that i'm using
{
$lookup: {
from: 'weeks',
localField: 'weeks',
foreignField: '_id',
as: 'weeks'
}
}
Result with this query
"weeks": [
{
"_id": "619e87d7b1bd6501c7aae286",
"name": "week-1",
"description": "Commodo in o.",
"course": "619e87d7b1bd6501c7aae281",
"days": [
"619e87dab1bd6501c7aae2a8",
"619e87dab1bd6501c7aae2a9",
"619e87dab1bd6501c7aae2aa",
"619e87dab1bd6501c7aae2ab",
"619e87dab1bd6501c7aae2ac",
"619e87dab1bd6501c7aae2ad"
],
"isCopy": false,
"__v": 0
},
{
"_id": "619e87d7b1bd6501c7aae287",
"name": "week-2",
"description": "Irure e.",
"course": "619e87d7b1bd6501c7aae281",
"days": [
"619e87dab1bd6501c7aae2db",
"619e87dab1bd6501c7aae2dc",
"619e87dab1bd6501c7aae2dd",
"619e87dab1bd6501c7aae2de",
"619e87dab1bd6501c7aae2df",
"619e87dab1bd6501c7aae2e0"
],
"isCopy": false,
"__v": 0
},]
In the above lookup localField weeks is an array of object Id's.
When i execute this code it works as expected, but i want to use the same functionality with pipeline.
Here's the code i wrote
{
$lookup: {
from: "weeks",
let: { wks: "$weeks" },
pipeline: [
{
$match: {
_id: {
$in: ["$$wks"]
}
}
}
],
as: "weeks"
}
}
Result with this query `weeks:[]`
When i run this, i don't get anything in output, the reason for that in my opinion is that weeks array is being interpreted as a string instead of object id.
How do i fix it now ...
Is this what you need ? Check my mongoplayground below, if not, make your own sample data and share a new link with me.
db.collection.aggregate([
{
$lookup: {
from: "weeks",
let: {
wks: "$weeks"
},
pipeline: [
{
$match: {
$expr: {
$in: [
"$_id",
"$$wks"
]
}
}
}
],
as: "weeks"
}
}
])
mongoplayground
I have a problem with how to lookup nested array, for example i have 4 collections.
User Collection
"user": [
{
"_id": "1234",
"name": "Tony",
"language": [
{
"_id": "111",
"language_id": "919",
"level": "Expert"
},
{
"_id": "111",
"language_id": "920",
"level": "Basic"
}
]
}
]
Language Collection
"language": [
{
"_id": "919",
"name": "English"
},
{
"_id": "920",
"name": "Chinese"
}
]
Job
"job": [
{
"_id": "10",
"title": "Programmer",
"location": "New York"
}
],
CvSubmit Collection
"cvsubmit": [
{
"_id": "11",
"id_user": "1234",
"id_job": "11"
}
]
And my query aggregation is:
db.cvsubmit.aggregate([
{
$lookup: {
from: "user",
localField: "id_user",
foreignField: "_id",
as: "id_user"
}
},
{
$lookup: {
from: "language",
localField: "id_user.language.language_id",
foreignField: "_id",
as: "id_user.language.language_id"
}
},
])
But the result is:
[
{
"_id": "11",
"id_job": "11",
"id_user": {
"language": {
"language_id": [
{
"_id": "919",
"name": "English"
},
{
"_id": "920",
"name": "Chinese"
}
]
}
}
}
]
I want the result like this, also showing all user data detail like name:
[
{
"_id": "11",
"id_job": "11",
"id_user": {
"_id": "1234",
"name": "Tony"
"language": [
{
"_id": "919",
"name": "English",
"Level": "Expert"
},
{
"_id": "920",
"name": "Chinese",
"level": "Basic"
}
]
}
}
]
Mongo Playground link https://mongoplayground.net/p/i0yCucjruey
Thanks before.
$lookup with user collection
$unwind deconstruct id_user array
$lookup with language collection and return in languages field
$map to iterate look of id_user.language array
$reduce to iterate loop of languages array returned from collection, check condition if language_id match then return name
db.cvsubmit.aggregate([
{
$lookup: {
from: "user",
localField: "id_user",
foreignField: "_id",
as: "id_user"
}
},
{ $unwind: "$id_user" },
{
$lookup: {
from: "language",
localField: "id_user.language.language_id",
foreignField: "_id",
as: "languages"
}
},
{
$addFields: {
languages: "$$REMOVE",
"id_user.language": {
$map: {
input: "$id_user.language",
as: "l",
in: {
_id: "$$l._id",
level: "$$l.level",
name: {
$reduce: {
input: "$languages",
initialValue: "",
in: {
$cond: [
{ $eq: ["$$this._id", "$$l.language_id"] },
"$$this.name",
"$$value"
]
}
}
}
}
}
}
}
}
])
Playground
You database structure is not accurate as per NoSQL, there should be max 2 collections, loot of join using $lookup and $unwind will cause performance issues.
I have a issue with the second $lookup when I am m using pipeline,but with localField and foreignField it works fine.
My collections bellow:
FeedCategories:
{"_id":{"$oid":"1"},"name":"XXX1","status":"active"}
{"_id":{"$oid":"2"},"name":"XXX2","status":"active"}
Feeds:
{ "_id": { "$oid": "1" }, "category": { "$oid": "1" }, "status": "published" }
{ "_id": { "$oid": "2" }, "category": { "$oid": "1" }, "status": "published" }
{ "_id": { "$oid": "3" }, "category": { "$oid": "2" }, "status": "published" }
FeedCategoriesUser
{ "_id": { "$oid": "1" }, "Feed_left_id": { "$oid": "1" }, "User_right_id": { "$oid": "2" }}
{ "_id": { "$oid": "2" }, "Feed_left_id": { "$oid": "2" }, "User_right_id": { "$oid": "2" }}
My mongoose code:
FeedCategoriesModel.aggregate([{
$lookup: {
from: 'feeds',
let: { categoryId: "$_id" },
pipeline: [
{ "$match": {
"$expr": { "$eq": [ "$category", "$$categoryId" ] },
"status": "published"
}}
],
as: 'feedsByCategory'
}
}, {
$lookup: {
from: 'feed_categories_user',
localField: 'feedsByCategory._id',
foreignField: 'Feed_left_id',
as: 'feedsByCategoryCompleted'
}
}]);
but when I am using pipeline in this way, I dont have any results in feedsByCategoryCompleted:
FeedCategoriesModel.aggregate([{
$lookup: {
from: 'feeds',
let: { categoryId: "$_id" },
pipeline: [
{ "$match": {
"$expr": { "$eq": [ "$category", "$$categoryId" ] },
"status": "published"
}}
],
as: 'feedsByCategory'
}
}, {
$lookup: {
from: 'feed_categories_user',
let: {feedid: "$feedsByCategory._id"},
pipeline:[{
"$match":{
"$expr": { "$eq": [ "$Feed_left_id", "$$feedid" ] }
}
}],
as: 'feedsByCategoryUsed'
}
}])
How to use piepeline for second $lookup in the above example?
In the second lookup, you need to use $in insted of $eq inside $match. The reason is, feedsByCategory is an array from the first lookup, you are assigning its _id to feedid. So its also an array. When you check possible values inside the match within the array, you need to use $in
{
$lookup: {
from: "FeedCategoriesUser",
let: {
feedid: "$feedsByCategory._id"
},
pipeline: [
{
"$match": {
"$expr": {
"$in": [
"$Feed_left_id",
"$$feedid"
]
}
}
}
],
as: "feedsByCategoryUsed"
}
}
Working Mongo playground