有一个.json数据提供的推文集合..
希望计算会话中的删除请求数:
db.tweets.find({"delete"}).count()
此语法不正确,因为SyntaxError: missing : after property id (shell):1
要执行更多find()
和count()
个操作,但错误是一致的。
这就是删除请求看起来像(其中“ ... ”是一系列字母和/或数字):
{
"_id" : ObjectId("…"),
"delete" : {
"status" : {
"id" : NumberLong("…"),
"user_id" : …,
"id_str" : "…",
"user_id_str" : "…"
}
}
}
答案 0 :(得分:5)
在find()
函数中,您必须传递一个对象。您错过了键/值,因为{"delete"}
不是有效对象。
我认为您希望获得具有删除密钥的文档数量。为此,您必须使用$exists
运算符并使用true
值。
db.tweets.find({ "delete": { $exists: true } }).count();
或直接
db.tweets.count({ "delete": { $exists: true } });
如果为true,则$ exists选择包含该字段的文档。如果为false,则查询仅返回不包含该字段的文档。不返回包含该字段但值为null的文档。