【问题标题】:What's wrong in syntax unable to fetch the data.(MongoDB)语法有什么问题无法获取数据。(MongoDB)
【发布时间】:2018-01-10 14:19:42
【问题描述】:
{
   "_id": ObjectId("7df78ad8902c"),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "school",
   "url": "http://www.school.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}

//我的代码获取值。

db.mycollection.find({"likes": {$lt:2000}}).pretty()

//给出错误

SyntaxError 无效属性 id @(shell) :1:32

//我的另一个代码

db.mycollection.find({$and:[{"by":"school"},{"title": "MongoDB Overview"}]}).pretty()

//给出这个错误

SyntaxError 无效属性 id @(shell) :1:22

我是 mongoDB 的新手,所以请帮助我了解错误在哪里以及如何解决。

提前致谢。

【问题讨论】:

  • 将 id 作为字符串传递:"_id": ObjectId("7df78ad8902c"),
  • 您有收藏中的文档吗?只有 fetch 给出错误?
  • 是的,只有 fetch 给出错误@Saravana

标签: mongodb


【解决方案1】:

好吧,insert 对于您发布的文档也给出了错误,因为您创建 _id 的方式正如 @skm 已经提到的那样。检查the documentation 以了解如何操作或完全省略,并且_id 将在插入 期间自动创建。第一个 find 运行没有错误,但什么也没找到,因为 likes 属性设置为字符串值 "100",并且您尝试查找 likes 小于 2000 整数的文档。第二个 find 实际上是找到文档。

【讨论】:

    猜你喜欢
    • 2013-04-04
    • 1970-01-01
    • 2016-04-11
    • 2014-07-10
    • 2015-11-30
    • 2019-05-23
    • 2018-02-18
    • 2020-08-22
    • 1970-01-01
    相关资源
    最近更新 更多