【发布时间】: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