我创建了一个关于使用已经解决的$ match的主题,但现在我陷入了一个更复杂的例子.例如,考虑我的Json的结构:
{
user: 'bruno',
answers: [
{id: 0, value: 3.5},
{id: 1, value: "hello"}
]
}
Run Code Online (Sandbox Code Playgroud)
我想添加或命令,对'value'中包含的值应用一些转换,只对标识符'id'等于0,例如sort方法.
请考虑以下数据:
db.my_collection.insert({ user: 'bruno', answers: [ {id: 0, value: 3.5}, {id: 1, value: "hello"}]})
db.my_collection.insert({ user: 'bruno2', answers: [ {id: 0, value: 0.5}, {id: 1, value: "world"}]})
Run Code Online (Sandbox Code Playgroud)
我试过用:
db.my_collection.aggregate ({$sort: {"answers": {"$elemMatch": {id: 0, value: -1}}}})
Run Code Online (Sandbox Code Playgroud)
但是,它没有用.预期结果是:{user:'bruno2'answers:[{id:0,value:0.5},{id:1,value:"world"}]},{user:'bruno'answers:[{id: 0,值:3.5},{id:1,value:"hello"}]})
谢谢.
正在阅读Mongo中的$ geoNear,但不能用于以下目的:我有以下JSON:
{
user: "bruno",
location: [30.45, 45.5],
other_fields...
}
Run Code Online (Sandbox Code Playgroud)
我想在我的应用程序管道中应用类似于以下命令的geoNear.可能吗?我想按地理位置字段排序.
db.places.find({coords:{$geoWithin:{$center:[[-23.017812,-45.544617],10]}}})
Run Code Online (Sandbox Code Playgroud)
谢谢.