按给定字段搜索嵌套对象的数组 [英] Search array of nested objects by given field

查看:70
本文介绍了按给定字段搜索嵌套对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有Room对象的以下结构.

I have the following structure of the Room object.

type Room struct {
Id          bson.ObjectId       `json:"id" bson:"_id,omitempty"`
Title       string              `json:"title" bson:"title"`
Description string              `json:"description" bson:"description,omitempty"`
Type        string              `json:"type" bson:"type,omitempty"`
AdminId     bson.ObjectId       `json:"admin_id" bson:"admin_id"`
CreatedOn   time.Time           `json:"created_on" bson:"created_on"`
Messages    []Message           `json:"messages" bson:"messages,omitempty"`}

消息"是对象的嵌套数组,具有以下结构

Where Messages is nested array of objects that has the following structure

type Message struct {
Id      bson.ObjectId   `json:"id" bson:"_id,omitempty"`
Text        string          `json:"text" bson:"text"`
Author      Author          `json:"author" bson:"author"`
CreatedOn   time.Time   `json:"createdon" bson:"created_on"`
Reply       []Message   `json:"reply" bson:"reply,omitempty"`}

我想通过房间集合中的消息执行搜索查询.我尝试使用"$ in" ,但没有帮助我.

I want to perform the search query by the messages in the collection of rooms. I tried using "$in" but I did not help me.

此外,我必须通过匹配值来搜索元素.我可以使用bson正则表达式来做到这一点.

Moreover, I have to search elements by matching values. I can do this using bson regular expressions.

&bson.RegEx{Pattern: textToFind, Options: "i"}

总结我需要通过Room文档中嵌套对象中的 Text 字段来搜索消息.

Summing up I need to search messages by the Text field in the nested object in the Room document.

P.S.抱歉,可能会出现错误,英语不是我的母语.

P.S. Sorry for possible mistakes, English is not my native language.

更新

基本上,我想在给定的房间中找到包含某些子字符串的所有消息.例如,搜索房间(聊天)"A"中包含某些文本"子字符串的所有消息.

Basically, I want to find all the messages in the given room that contains some substring. For example, search for all messages in the room (chat) 'A' that contains 'some text' substring.

推荐答案

您可以尝试以下mongo shell聚合管道.

You can try the below mongo shell aggregation pipeline.

$ match 是某个房间属性(例如 _id ).

$matches on some room attribute (ex _id).

$ unwind 消息(将 messages 数组转换为object)在房间中.

$unwind messages( transform messages array into object ) in the room.

$ match es针对 text 字段来过滤消息.

$matches on input regex against text field to filter messages.

$ group 将消息对象放回 messages 数组.

$groups the message objects back into messages array.

$ project 排除 _id 并仅包含 messages 进行输出.

$project to exclude _id and include only messages for output.

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等效项.

Below is untested mgo equivalent.

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)

这篇关于按给定字段搜索嵌套对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆