在mongodb查询中添加自定义字段 [英] Add custom fields in mongodb query

查看:1494
本文介绍了在mongodb查询中添加自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对mongodb中的集合有这种情况的疑问

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

这是我的帖子集

{ 

  "_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"
   }
]

}

我执行了一个查询,传递了正在查看页面的clientId和一个用于返回多边形区域中所有帖子的多边形,并且我想添加一个自定义字段,因为我想知道此信息:

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 :

  • 如果Post是我的(clientId == creatorId)

  • If Post is mine ( clientId == creatorId)

  • 如果有回复(replies.count> 0)

如果帖子不是我的

  • 如果我的回复中有一个(有一个包含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

}

谢谢谢谢谢谢

L

推荐答案

好的,因此find无法做到这一点.但是使用 汇总 .因此,假设您在您的 ID为

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
    }}
])

除其他事项外,您的自定义字段"是通过使用逻辑true,否则为false.

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

有关语言的详细信息,请参见驱动程序文档(此处显示的是所有人的JSON),但是基本上,您正在将BSON文档形成为管道的每个阶段.

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.

这篇关于在mongodb查询中添加自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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