选择一个具有有限元素数量的数组的文档。

huangapple go评论138阅读模式
英文:

Select one document with array with limited number of elements

问题

我有以下 Room 类型的结构:

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"`
}

类型结构 Message 是嵌套的,具有以下结构:

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"`
}

使用这段代码,我可以提取集合的所有字段。

room := &Room{}
roomsCollection := session.DB(config.Data.DB.Database).C("Rooms")
err := roomsCollection.Find(bson.M{"_id": room_id}).One(room)
if err != nil {
    panic(err)
    return nil, err
}

这将返回给定文档中所有带有消息的房间。问题是,我可以限制每个文档中嵌套的 Messages 数组的长度吗?

英文:

I have the following structure of the Room type:

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"`}

The type struct Message is embedded, which 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"`}

Using this code I can extract all the fields of the collection.

room := &Room{}
roomsCollection := session.DB(config.Data.DB.Database).C("Rooms")
err := roomsCollection.Find(bson.M{"_id": room_id}).One(room)
if err != nil {
	panic(err)
	return nil, err
}

This gives the rooms with all the messages in the given documents.
The question is, can I limit the length of nested Messages array in each document?

答案1

得分: 2

所以,解决方案非常简单。我们可以使用$slice来执行此操作。结果查询如下所示:

roomsCollection.Find(bson.M{"_id": room_id})).Select(bson.M{"messages": bson.M{"$slice": 5}}).One(room)

特别感谢@Veeram提供的正确答案。

英文:

So, the solution was really simple. To perform this we can use $slice.
The result query will be as following:

roomsCollection.Find(bson.M{"_id": room_id})).Select(bson.M{ "messages": bson.M{ "$slice": 5} } ).One(room)

Special thanks to @Veeram for correct answer.

huangapple
  • 本文由 发表于 2017年3月22日 23:25:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/42956009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定