【问题标题】:Mongodb: get only array of post comments from this objectMongodb:仅从此对象获取帖子评论数组
【发布时间】:2018-05-22 09:22:56
【问题描述】:

到目前为止,我实现的是我在对象内部得到了数组,我直接想要数组。

这是我的对象:

{
    "_id" : ObjectId("5a28e2a1bf22d2a9ddb8f5d5"),
    "description" : "My Post",
    "postLikes" : [ 
        {
            "likesCount" : 0,
            "likeByEmail" : "a@a.com",
            "createdOn" : NumberLong(1512631701702),
            "updatedOn" : NumberLong(0)
        }
    ],
    "postComments" : [ 
        {
            "comment" : "This is first comment",
            "likeByEmail" : "a@a.com",
            "createdOn" : NumberLong(1512643824764),
            "updatedOn" : NumberLong(1512643824764)
        }
    ],
    "createdOn" : NumberLong(0),
    "updatedOn" : NumberLong(0)
}

我的查询是:

db.user_posts.find({_id: ObjectId("5a28e2a1bf22d2a9ddb8f5d5")}, {postComments:1})

结果是:

{
    "_id" : ObjectId("5a28e2a1bf22d2a9ddb8f5d5"),
    "postComments" : [ 
        {
            "comment" : "This is first comment",
            "likeByEmail" : "a@a.com",
            "createdOn" : NumberLong(1512643824764),
            "updatedOn" : NumberLong(1512643824764)
        }
    ]
}

我想要的是:

 [ 
        {
            "_id" : null,
            "comment" : "This is first comment",
            "likeByEmail" : "a@a.com",
            "createdOn" : NumberLong(1512643824764),
            "updatedOn" : NumberLong(1512643824764)
        }
    ]

我也在 Spring Boot 中使用 MongoRepository 并且那里的查询是:

@Query(value = "{'_id' : ?0}, {'postComments':1}")

它给了我同样的结果。

【问题讨论】:

    标签: mongodb spring-boot mongodb-query


    【解决方案1】:

    你可以加"_id":false

    db.user_posts.find({
        _id: ObjectId("5a28e2a1bf22d2a9ddb8f5d5")
    }, {
        postComments: 1,
        "_id": false
    })
    

    "_id":0

    db.user_posts.find({
        _id: ObjectId("5a28e2a1bf22d2a9ddb8f5d5")
    }, {
        postComments: 1,
        "_id": 0
    })
    

    如果您只想要文档的一部分,那么您必须使用聚合框架。幸运的是,上面有弹簧支撑。

    正如 vaibhav 已经提到的 find() 返回的内容。

    您可以通过这样的聚合来解决您的问题:

    db.user_posts.aggregate([
    { $match: {_id: ObjectId("5a28e2a1bf22d2a9ddb8f5d5")}},
    { $unwind: "$postComments"},
    { $project: {
        _id: 0,
        comment: "$postComments.comment",
        likeByEmail: "$postComments.likeByEmail",
        createdOn: "$postComments.createdOn",
        updatedOn: "$postComments.updatedOn"
      }
    }
    ])  
    

    按照@Veeram 的建议,您也可以这样做:

    db.user_posts.aggregate([
    { $match: {_id: ObjectId("5a28e2a1bf22d2a9ddb8f5d5")}},
    { $unwind: "$postComments"},
    { $replaceRoot: {"newRoot":"$postComments"}}
    ])  
    

    【讨论】:

    • { "postComments" : [ { "_id" : null, "comment" : "这是第一条评论", "likeByEmail" : "a@a.com", "createdOn" : NumberLong( 1512643824764), "updatedOn" : NumberLong(1512643824764) } ] } 它给了我这个结果。我直接需要数组
    • [ { "_id" : null, "comment" : "这是第一条评论", "likeByEmail" : "a@a.com", "createdOn" : NumberLong(1512643824764), "updatedOn " : NumberLong(1512643824764) } ] 像这样
    • 我用聚合框架更新了答案,看看吧。
    • 您也可以在 3.4 版本中将$project stage 替换为$replaceRoot。使您免于显式映射所有字段。类似{ $replaceRoot: {"newRoot":"$postComments"}}
    【解决方案2】:

    使用 find 您将永远无法获得数组,因为根据定义:

    find() 方法“返回文档”,该方法实际上是 将光标返回到文档。

    您可以将投影应用于结果集,但它基本上总是会返回一个文档,不幸的是该文档始终是 json 并将数组作为属性嵌入。

    同样适用于插入函数,您不能单独插入数组,它必须是有效的 JSON。

    > db.arrCollection.insert(["name":"myName", "age": 100]);
    2017-12-08T13:02:30.492+0530 E QUERY    SyntaxError: Unexpected token :
    

    【讨论】:

      【解决方案3】:

      你可以试试这个

      db.user_posts.find({_id: ObjectId("5a28e2a1bf22d2a9ddb8f5d5")}, {postComments:1}).then(function(u){return u.postComments;})

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多