你好,我在MongoDB中一個帖子集合,其中有一個作者字段,當我運行以下命令:MongoDB的項目計數與條件對象
db.posts.aggregate([ {$project:{ size: {$size: {$ifNull:["$authors", []] }}}} ])
我得到的結果這樣的:
{ "_id" : ObjectId("58c917fe48ad625ee8f49714"), "size" : 30 }
{ "_id" : ObjectId("58c91b83895efc5f0f67ba1a"), "size" : 0 }
{ "_id" : ObjectId("58c91cfd2971c05f310fccb8"), "size" : 30 }
{ "_id" : ObjectId("58c91eb7a826965f85571656"), "size" : 30 }
{ "_id" : ObjectId("58c921a1cb2bc85fa77e593a"), "size" : 30 }
如何計算大小不等於0時的次數? 所以在這種情況下,結果將是4.
我試過「db.posts.aggregate([{$ project:{size:{$ size:{$ not:{」$ authors「:0}}} }}]) 「沒有成功......
你可以使用普通的查詢。 'db.posts.count({「authors」:{$ exists:true,$ ne:[]}})' – Veeram
這個好友 – Defoe