请试试这个。下面的查询将在顶部为您提供“banga”以及之后的所有其他文档。
注意:-
请注意,查询不会按城市对数据进行排序。它只是返回顶部的“banga”文档。 "city" 不等于 "banga" 的文档可以按任何顺序排列。
查询:-
您可能需要在以下查询中相应地更改集合名称。
db.collection.aggregate([
{ $project: {_id : 1, "name": 1, "city" : 1,
isRequiredCity: { $cond: { if: { $eq: [ "$city", "banga" ] }, then: 0, else: 1 } }} },
{$sort : {"isRequiredCity" : 1} }
]);
输出:-
/* 1 */
{
"_id" : ObjectId("58342ccaba41f1f22e600c67"),
"name" : "sass",
"city" : "banga",
"isRequiredCity" : 0
}
/* 2 */
{
"_id" : ObjectId("58342ccaba41f1f22e600c6a"),
"name" : "pass",
"city" : "banga",
"isRequiredCity" : 0
}
/* 3 */
{
"_id" : ObjectId("58342ccaba41f1f22e600c68"),
"name" : "kanga",
"city" : "runga",
"isRequiredCity" : 1
}
/* 4 */
{
"_id" : ObjectId("58342ccaba41f1f22e600c69"),
"name" : "jass",
"city" : "canga",
"isRequiredCity" : 1
}
/* 5 */
{
"_id" : ObjectId("58342ccaba41f1f22e600c6b"),
"name" : "dass",
"city" : "tunga",
"isRequiredCity" : 1
}