Node.js 使用nodejs访问mongodb中.find和.forEach返回的对象/游标的属性

Node.js 使用nodejs访问mongodb中.find和.forEach返回的对象/游标的属性,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,改变了模式,一切都变得疯狂(参见下面的更改)。现在从.find()和cursor.forEach()访问属性在后端返回“undefined”: 编辑:已找到 .find().lean().exec(callback) 允许访问回调中的属性,但很难对它们执行任何操作,并且允许通过执行 doc._doc.property 在回调中工作: .find(function(err,doc){for (i in docs){doc=docs[i]; console.log(doc._doc.prop

改变了模式,一切都变得疯狂(参见下面的更改)。现在从.find()和cursor.forEach()访问属性在后端返回“undefined”:

编辑:已找到

.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循环记录的sing之一--

编辑:

叶苏:

collection.find(query).exec(function(err,docs) {
     docs.forEach(function(singleDoc) {
        console.log(singleDoc._doc.property); //DEFINED, bad boyz 4 lyfe *_*
   })
})
因此,我最终决定将cursor.forEach返回的文档的该死的键记录到console.log中

这还返回定义的:

 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 :(
})
collection.find(query).exec(函数(err,docs){
if(err){returnerrorhandler(err)};
var doc=docs[0];
var captainKeyes=Object.keys(请求主体);
对于(k=0;k
我将集合R的模式更改为具有一些键,将其从一个空对象更改为strict:false

from {{},strict:false} to {{name: String, rating: Number, person: String},strict:false}
现在我不必使用_doc,wooohooo,所有的查询都能正常工作


故事的寓意是,我真的不知道如何正确地实现无模式的集合,然后stuff got cray

你能为
控制台共享示例输出吗。log(sing)
?sing是r.e路径中的文档,或者是r集合中的新模式中的文档,在创建集合时:{{u-id:1254357653,name:peep,rating:6,type:some-type}(其中一个记录为dat数组的长度)。当没有创建R集合时,就像在我的第一个架构设计中一样,sing是相同的,只是没有_-id字段。
from {{},strict:false} to {{name: String, rating: Number, person: String},strict:false}