
您可以尝试下面的mongo shell聚合管道。
$match在某些房间属性(例如
_id)上。
$unwind消息(将
messages数组转换为object)在房间里。
$match在输入正则表达式上针对
text要过滤的字段
messages。
$group将消息对象放回到
messages数组中。
$project排除
_id并仅包括
messages用于输出。
db.collection.aggregate({$match:{"_id":roomid}}, {$unwind:"$messages"}, {$match:{"messages.text": { $regex: /textToFind/i } }},{$group:{_id:null,messages:{$push:"$messages"}}}, {$project:{_id:0, messages:1}})以下是未经测试的mgo当量。
match1 := bson.M{ "$match": bson.M{ "_id": roomid, },}unwind := bson.M{ "$unwind": "$messages",}match2 := bson.M{ "$match": bson.M{"messages.text": &bson.RegEx{Pattern: textToFind, Options: "i"}},}group := bson.M{ "$group": bson.M{ "_id": null, "messages": bson.M{ "$push": "$messages", }, },}project := bson.M{ "$project": bson.M{ "_id": 0, "messages":1, },}all := []bson.M{match1, unwind, match2, group, project}pipe := collection.Pipe(all)result := []bson.M{}err := pipe.All(&result)欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)