I want to get the data records from mongodb document using $or and $lookup. Here is a query I make for assertion:-
pipeline1 := []bson.M{
{"$lookup": bson.M{"from": "comment", "localField": "_id", "foreignField": "blog_id", "as": "comments"}},
{"$addFields": bson.M{"comments": bson.M{"$size": "$comments"}}},
}
pipe1 := getCollection.Pipe(pipeline1)
In the above query it will first count the records and add the field with the particular data. But now I have a little change in the query is that now there are two more parameters i have to search with this using $or for regular expression I want that the function will doing the same thing but while getting the records if the user will entered any parameter then it will fetch the record by using that keyword and also make the count of that data.
Edited
I have to combined the above query with the below query:-
query["$or"] = []bson.M{
bson.M{"title": bson.RegEx{"(?i).*" + value + ".*", "i"}},
bson.M{"type": bson.RegEx{"(?i).*" + value + ".*", "i"}},
bson.M{"author": bson.RegEx{"(?i).*" + value + ".*", "i"}},
bson.M{"tags": bson.RegEx{"(?i).*" + value + ".*", "i"}},
}
I'm combining it like this but it return Nothing.
pipeline1 := []bson.M{
{"$or": []bson.M{
bson.M{"_id": bson.M{"$in": ids}},
bson.M{"type": bson.RegEx{"(?i).*" + types + ".*", "i"}},
bson.M{"category": bson.RegEx{"(?i).*" + category + ".*", "i"}},
bson.M{"author": bson.RegEx{"(?i).*" + keyword + ".*", "i"}},
bson.M{"title": bson.RegEx{"(?i).*" + keyword + ".*", "i"}},
bson.M{"tags": bson.RegEx{"(?i).*" + tag + ".*", "i"}}}},
{"$lookup": bson.M{"from": "comment", "localField": "_id", "foreignField": "blog_id", "as": "comments"}},
{"$addFields": bson.M{"comments": bson.M{"$size": "$comments"}}},
}
pipe1 := getCollection.Pipe(pipeline1)
Latest Edit
AS @kmdreko say I make a similar like that but does not works:-
query := bson.M{}
query["$or"] = []bson.M{
bson.M{"_id": bson.M{"$in": ids}},
bson.M{"type": bson.RegEx{"(?i).*" + types + ".*", "i"}},
bson.M{"category": bson.RegEx{"(?i).*" + category + ".*", "i"}},
bson.M{"author": bson.RegEx{"(?i).*" + keyword + ".*", "i"}},
bson.M{"title": bson.RegEx{"(?i).*" + keyword + ".*", "i"}},
bson.M{"tags": bson.RegEx{"(?i).*" + tag + ".*", "i"}},
}
fmt.Println(query)
pipeline1 := []bson.M{
{"$lookup": bson.M{"from": "comment", "localField": "_id", "foreignField": "blog_id", "as": "comments"}},
{"$addFields": bson.M{"comments": bson.M{"$size": "$comments"}}},
{"$match": query},
}
fmt.Println(pipeline1)
pipe1 := getCollection.Pipe(pipeline1)