1

节省:

   var pageListSchema = new Schema({
    pid:String,
    name:String,
    eName:String,
    pages:[{name:String,id:String,type:String}]
    });
    var pageList = db.model('pageList',pageListSchema);
    var p = new pageList({pid:getId,name:getName,eName:getEName,pages:[{name:"html",id:"0",type:"0"}]});
    p.save();

读:

pageList.find({pages:[{id:"0"}]},function(err,data){
    console.log(data);
});
pageList.find({pages:{$elemMatch:{id:"0"}}},function(err,data){
    console.log(data);
});

结果:

[ { _pid: '510a3e793f30c5980f000001'
name: 'cc',
eName: 'cc',
_id: 510a3e803f30c5980f000002,
__v: 0,
pages: [ '[object Object]' ] } ]

有两种读取数据的方法,但是为什么我得到的是字符串而不是对象

4

1 回答 1

0

console.log调用util.inpsect将传递的data对象格式化为字符串。默认情况下,该方法仅递归到对象深处的两个级别,因此pages元素的内容没有被格式化。

要获得所有级别的完整输出data

console.log(util.inspect(data, false, null));
于 2013-01-31T14:05:52.200 回答