0
find
可以排除操作字段,但是如果我想要執行find
然後sort
和緊接在projection
之後。你知道有什麼竅門嗎?如何在排序後在MongoDB中投影?
doc:fields {Object}, the fields to return in the query. Object of fields to include or exclude (not both), {‘a’:1}
find
可以排除操作字段,但是如果我想要執行find
然後sort
和緊接在projection
之後。你知道有什麼竅門嗎?如何在排序後在MongoDB中投影?
doc:fields {Object}, the fields to return in the query. Object of fields to include or exclude (not both), {‘a’:1}
您可以運行的條件,預測和排序通常查找查詢。我想你想排序一個你不想投射的領域。但不要擔心,即使在未投影之後,您也可以在該字段上排序。
如果您顯式選擇排序字段的投影爲「0」,那麼您將無法執行該查找查詢。
//This query will work
db.collection.find(
{_id:'someId'},
{'someField':1})
.sort('someOtherField':1)
//This query won't work
db.collection.find(
{_id:'someId'},
{'someField':1,'someOtherField':0})
.sort('someOtherField':1)
但是,如果您仍未獲得所需結果,請查看MongoDB Aggregation Framework!
這裏是聚集示例查詢根據您的要求
db.collection.aggregate([
{$match: {_id:'someId'}},
{$sort: {someField:1}},
{$project: {_id:1,someOtherField:1}},
])