你好,我在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