mongodb自定义字段_在mongodb查询中添加自定义字段

I have a question for this situation with a collection in mongodb

this is my Post collection

{

"_id" : ObjectId("530f67584fb1a510bc18c03f"),

"creatorId" : "5f6f2c26-4101-4eae-90d1-20d109bea925",

"creationDate" : ISODate("0001-01-01T00:00:00Z"),

"category" : 23,

"location" : [

-60.67045855832774,

52.86982649605247

],

"replies" :

[

{

"_id" : ObjectId("531acfc34fb1a50edc86fdcb"),

"creatorId" : "0891f887-a6bc-4183-be10-2653b7b45e79"

},

{

"_id" : ObjectId("531acfc34fb1a50edc86fdcb"),

"creatorId" : "0891f887-a6bc-4183-be10-2653b7h76s22"

}

]

}

I perform a query passing clientId who is watching the page and a polygon for return all posts in polygon area and I want to add a custom fields because I want to know this info :

If Post is mine ( clientId == creatorId)

If there are replies (replies.count > 0 )

If post is not mine

If there are one my reply ( there is one of reply with replies.creatorId == clientId)

and i want to do this in only one query because I want to view all list of posts and know what kind of different colour mark the post.

The result that I want is something like this (if is it possible) or something else that returns the infos required.

{

"_id" : ObjectId("530f67584fb1a510bc18c03f"),

"category" : 23,

"location" : [

-60.67045855832774,

52.86982649605247

],

"isMine" : true,

"replies" : 20,

"OneAnswerIsMine": false

}

Thanks Thanks Thanks in advance

L

解决方案

Okay, so find cannot do this. But there is a kind of obscure way to get the result using aggregate. So assuming you have a creatorId variable somewhere that is your Id:

db.collection.aggregate([

// Match your posts

{ "$match": { "creatorId": creatorId },

// Unwind the array

{ "$unwind": "$replies" },

// Project the fields you want, notice the logical conditions

{ "$project": {

"category": 1,

"location": 1,

"isMine": { "$eq": [ "$creatorId", creatorId ] },

"answerIsMine": { "$eq": [

"$replies.creatorId",

creatorId

]}

}},

// Group on the unique document, get sum and max value

{ "$group": {

"_id": {

"_id": "$_id",

"category": "$category",

"location": "$location",

"isMine": "$isMine",

},

"replies": { "$sum": 1 },

"OneAnswerIsMine": { "$max": "$answerIsMine" }

}},

// Nicer Output

{ "$project": {

"_id": "$_id._id",

"category": "$_id.category",

"location": "$_id.location",

"isMine": "$_id.isMine",

"replies": 1,

"OneAnswerIsMine": 1

}}

])

Apart from the other things, your "custom fields" were generated by using the logical $eq operator. Here we compare the field value of creatorId to the variable value for you that you have stored. When they are "equal" then the condition is true, otherwise it is false.

So, therefore welcome to aggregation. That is a very powerful tool to get the results that you want and you can not only "group" things together, but otherwise transform the document into the form that you want

See your driver documentation for the language details ( presented here is JSON for everyone ), but you are basically forming BSON documents as each stage of the pipeline.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值