【问题标题】:Accessing properties of object/cursor returned from .find and .forEach in mongodb with nodejs使用nodejs访问从mongodb中的.find和.forEach返回的对象/游标的属性
【发布时间】:2015-06-19 02:20:48
【问题描述】:

更改了架构,一切都变得疯狂(请参阅下面的更改)。现在从 .find() 和 cursor.forEach() 访问属性在后端返回“未定义”:

编辑:找到了

.find().lean().exec(callback) 

允许访问回调中的属性,但很难对它们做任何事情,并且通过这样做来访问属性

doc._doc.property 

在回调中工作:

.find(function(err,doc){for (i in docs){doc=docs[i]; console.log(doc._doc.property)}} 
and .forEach(function(doc){console.log(doc._doc.property)}:

我的架构曾经是这样的

用于收集人

{
  name: String,
  v: Types.ObjectId, ref: V //shorthand
  r: {
    e: [{}], 
    u: [{}]  
  }
}

现在是这个样子

var people = new mongoose.Schema (
 {
  name: String,
  v: {type: mongoose.Schema.Types.ObjectId, ref: V} 
  r: {
    e: [{type: mongoose.Schema.Types.ObjectId, ref: R}], 
    u: [{type: mongoose.Schema.Types.ObjectId, ref: R}]  
   }
 }
)
mongoose.model('people',people);

用于收集r

var collR = new mongoose.Schema({}, {strict:false})
mongoose.model('R',collR)

nodejs 控制器 1:

module.exports.getProducts = function (req, res) {

 people.find(req.query)
  .populate('v r.e r.u')
  .exec(function (err, data) {
      if (err) {sendJsonResponse(res,400,err)} 
      else {
       data.forEach(function(single){
         single.r.e.forEach(function(sing){
          console.log(sing) //defined, and i saw rating, and its defined
          console.log(sing.rating); //undefined
          // do something with sing.rating but it's undefined here
          })
       })

     sendJsonResponse(res,200,data);   //not undefined on frontend success callback
     }
});


};

节点控制器 2:

    module.exports.getProducts = function (req, res) {

 people.find(req.query)
  .populate('v r.e r.u')
  .exec(function (err, data) {
      if (err) {sendJsonResponse(res,400,err)} 
      else {
       data.forEach(function(single){
        R.find({person: single.name}, function (err, dat) {
         dat.forEach(function(sing){
          console.log(sing) //defined and rating defined
          console.log(sing.rating);  //undefined ugh.
          //do something with rating but cant bc undefined here
          })
        })
       })

      //if i send data back here, in success callback, data[i].r.e[j].rating is defined for all i and j, whaaa!?!
     }
});


};

从 cursor.forEach 循环中记录的歌曲之一---

{_id: 1254357653, name: peep, rating: 6, type: some type} 

编辑:

是这样的:

collection.find(query).exec(function(err,docs) {
     docs.forEach(function(singleDoc) {
        console.log(singleDoc._doc.property); //DEFINED, bad boyz 4 lyfe *_*
   })
})

所以我最终决定 console.log 从 cursor.forEach 返回的文档的该死键

这也返回定义:

 collection.find(query).lean().exec(function(err,docs) {
 console.log(docs[i].property); //for all i, THEY'RE DEFINED!!!!! wooo
})

现在当我尝试在查找中进行更新时会弹出另一个问题

    collection.find(query).exec(function(err,docs) {
      if (err) {return errorHandler(err)};
      var doc = docs[0];
      var captainKeyes = Object.keys(req.body);
      for (k = 0 ; k < captainKeyes.length ; k++) {
        //update the doc key/value pairs with what is sent in req.body
        doc._doc[captainKeyes[k]] = req.body[captainKeyes[k]];
       //from above, learned to access properties captainKeyes[k], you have to first access
      //the hidden property _doc to get to actual doc

      }
      doc.save()  
       //old doc is still in db, damn. and all this used to work before 
       //we added that R collection :(
})

【问题讨论】:

  • 你能分享console.log(sing)的示例输出吗?
  • sing 是来自 r.e 路径或来自 R 集合的新模式中的文档,当集合被创建时:{_id: 1254357653, name: peep, rating: 6, type: some type} (记录 dat 数组的长度)。当没有创建 R 集合时,就像在我的第一个模式设计中一样,sing 是相同的,除了没有 _id 字段。

标签: node.js mongodb mongoose


【解决方案1】:

我将集合 R 的架构更改为具有一些键,将它从一个空对象更改为 strict: false。

from {{},strict:false} to {{name: String, rating: Number, person: String},strict:false}

现在我不必使用 _doc,wooohoooo,所有查询再次正常工作。

故事的寓意,我真的不明白如何正确实现无模式集合,然后事情就变得糟糕了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2011-03-29
    • 1970-01-01
    相关资源
    最近更新 更多