dongxu1668 2018-09-12 06:12
浏览 273

如何使用go在mongodb查询中将$ or与$ lookup一起使用?

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)
  • 写回答

1条回答 默认 最新

  • doutan6286 2018-09-12 06:42
    关注

    If the fields you're filtering are on the root document, then the first stage should be a $match stage. So the pipeline will look like this:

    { $match: { $or: [...] } }, // your query
    { $lookup: {...} },         // same as before
    { $addFields: {...} }       // same as before
    

    Its surprising your query doesn't return an error as $or is not an aggregation pipeline stage.

    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功