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

时间:2013-05-19 17:50:02

标签: json mongodb count find syntax-error

有一个.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" : "…"
        }
    }
}

1 个答案:

答案 0 :(得分:5)

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

我认为您希望获得具有删除密钥的文档数量。为此,您必须使用$exists运算符并使用true值。

db.tweets.find({ "delete": { $exists: true } }).count();

或直接

db.tweets.count({ "delete": { $exists: true } });

来自documentation

  如果为true,则

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