【问题标题】:CoffeeScript and MongoDB unable to find objectCoffeeScript 和 MongoDB 找不到对象
【发布时间】:2014-02-11 10:58:31
【问题描述】:

各位, 无法找到本地 mongo 实例中确实存在的文档:

collection = db.collection('myCollection')
console.log "looking for id",accountId

collection.find
    _id: "52d9a0dec78792f877afa0a1"
, (err, result) ->
    if err or not result
        console.log "Unable to find record: #{err}"
        callback 'find did not work: #{err}'
    else
        console.log result
        result.nextObject (account) ->
            console.log account

输出:

Listening on 3000...
Account Name undefined
Account Balance undefined
Account Id undefined
something
looking for id 52d9a0dec78792f877afa0a1
{ toArray: [Function],
  each: [Function],
  next: [Function],
  nextObject: [Function],
  setReadPreference: [Function],
  batchSize: [Function],
  count: [Function],
  stream: [Function],
  close: [Function],
  explain: [Function],
  isClosed: [Function],
  rewind: [Function],
  limit: [Function],
  skip: [Function],
  hint: [Function],
  maxTimeMS: [Function],
  sort: [Function],
  fields: [Function] }
null

【问题讨论】:

  • 如果您的文档的_id 是一个ObjectID,那么您需要先从id 字符串创建一个,然后再将其传递给find
  • @JohnnyHK 不确定...这是文档的样子gist.github.com/vasiliyb/8546795

标签: node.js mongodb coffeescript


【解决方案1】:

来自docs

下一个对象

从光标处获取下一个文档。

下一个对象(回调) 参数:回调(函数) this 将在执行此方法后被调用。 第一个参数将包含错误对象,而第二个参数将包含返回结果中的文档,如果没有更多结果,则为 null。

来自github readme

var idString = '4e4e1638c85e808431000003';  
collection.findOne({_id: new ObjectID(idString)}, console.log)  // ok  
collection.findOne({_id: idString}, console.log)  // wrong! callback gets undefined  

解决方案

  • 在查询和 _id 之前,您应该将其转换为 ObjectID
  • 在 node.js 回调中,按照惯例,第一个参数是错误对象。

所以你应该这样写:

collection.find
    {_id: db.ObjectID("52d9a0dec78792f877afa0a1")}
, (err, result) ->
    if err or not result
        console.log "Unable to find record: #{err}"
        callback 'find did not work: #{err}'
    else
        console.log result
        result.nextObject (err, account) ->
            console.log account

我觉得用findOne比较好:

collection.findOne {_id: db.ObjectID("52d9a0dec78792f877afa0a1")}, (err, result) ->
  if err or not result
    console.log "Unable to find record: #{err}"
    callback 'find did not work: #{err}'
  else
    console.log result
    callback 'did work'

【讨论】:

  • OP 似乎确实知道result 是一个光标,因为他们在上面调用nextObject
  • @IlanFrumer 不幸的是,我得到了TypeError: Object has no method 'toArray'
【解决方案2】:

您的文档的_idObjectID,因此您需要将id 字符串转换为正确的类型,然后再将其传递给find

collection.find
    _id: mongodb.ObjectID "52d9a0dec78792f877afa0a1"
, (err, result) ->

【讨论】:

    【解决方案3】:
    cursor = collection.find
        _id: mongodb.ObjectID accountId
    cursor.toArray (err, result) ->
                if err or not result
                    console.log "Unable to find record: #{err}"
                    callback 'find did not work: #{err}'
                else
                    console.log result
                    #result.toArray (account) ->
                    #    console.log "Account is ", account
                    callback null, result
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 2012-11-01
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多