MongoDB find().count() - SyntaxError:missing:属性id(shell)之后:1

sou*_*rgy 0 json count syntax-error find mongodb

有一个.json数据提供的推文集合..

希望计算会话中的删除请求数:

db.tweets.find({"delete"}).count()
Run Code Online (Sandbox Code Playgroud)

这种语法不正确,因为 SyntaxError: missing : after property id (shell):1

有更多find()count()操作执行,但错误是一致的.

这就是删除请求的样子(其中" ...... "是一系列字母和/或数字):

{
    "_id" : ObjectId("…"),
    "delete" : {
        "status" : {
            "id" : NumberLong("…"),
            "user_id" : …,
            "id_str" : "…",
            "user_id_str" : "…"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ion*_*zău 5

find()函数中,您必须传递一个对象.您错过了键/值,因为{"delete"}它不是有效对象.

我想你想获得具有删除键的文档数量.为此,您必须使用值为$existsoperator的运算符true.

db.tweets.find({ "delete": { $exists: true } }).count();
Run Code Online (Sandbox Code Playgroud)

或直接

db.tweets.count({ "delete": { $exists: true } });
Run Code Online (Sandbox Code Playgroud)

来自文档:

如果为true,$ exists将选择包含该字段的文档.如果为false,则查询仅返回不包含该字段的文档.不返回包含该字段但值为null的文档.