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 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)