Add custom fields in mongodb query

≯℡__Kan透↙ 提交于 2019-12-23 06:04:30

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/22503439/add-custom-fields-in-mongodb-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!