列出来自mongo集合的值数组

时间:2016-02-02 13:28:18

标签: node.js mongoose

我有一个像这样的mongo文件:

"_id" : ObjectId("56af2ca2eb91bd4721443037"),
    "username" : "xxxxxx",
    "password" : "02adcec2263d2127269fcd769c33f029",
    "firstName" : "yyyyy",
    "lastName" : "zzzz",
    "email" : "xxxxyyyy.m@gmail.com",
    "userVerified" : "1",
    "userType" : "user",
    "__v" : 0,
    "userGroup" : [
        {
            "groupName" : "nodeJs",
            "groupAssigned" : "No",
            "_id" : ObjectId("56b086ccd1bc72eb21d99c62")
        },
        [
            {
                "groupName" : "php",
                "groupAssigned" : "No",
                "_id" : ObjectId("56b0880d13c9fc4c22a1d291")
            }
        ],
        [
            {
                "groupName" : "android",
                "groupAssigned" : "No",
                "_id" : ObjectId("56b0883513c9fc4c22a1d292")
            }
        ],
        [
            {
                "groupName" : "ios",
                "groupAssigned" : "No",
                "_id" : ObjectId("56b0883913c9fc4c22a1d293")
            }
        ],
        [
            {
                "groupName" : "dotNet",
                "groupAssigned" : "No",
                "_id" : ObjectId("56b0884213c9fc4c22a1d294")
            }
        ],
        [
            {
                "groupName" : "testing",
                "groupAssigned" : "No",
                "_id" : ObjectId("56b0885913c9fc4c22a1d295")
            }
        ],
        [
            {
                "groupName" : "design",
                "groupAssigned" : "No",
                "_id" : ObjectId("56b0886213c9fc4c22a1d296")
            }
        ]
    ]
}

和我的架构声明如:

var userSchema  =   new mongoose.Schema({
    username    : {type:String},
    password    : {type:String},
    firstName   : {type:String},
    lastName    : {type:String},
    email       : {type:String},
    userVerified: {type:String},
    userType    : {type:String},
    userGroup   : [{groupName:{type:String},groupAssigned:{type:String}}]
},{collection:'user'});

var user    =   mongoose.model('user',userSchema);

现在我要列出所有数组元素(这里:userGroup)和用户名,我尝试过:

user.find().select('userGroup').exec(function(err,rows){
    for (var i in rows)
    console.log(rows[i].username+' '+rows[i].userGroup);
});

它显示如下输出:

    **undefined** { _id: 56b086ccd1bc72eb21d99c62,
  groupAssigned: 'No',
  groupName: 'nodeJs' },{ '0': 
   { _id: 56b0880d13c9fc4c22a1d291,
     groupAssigned: 'No',
     groupName: 'php' } },{ '0': 
   { _id: 56b0883513c9fc4c22a1d292,
     groupAssigned: 'No',
     groupName: 'android' } },{ '0': 
   { _id: 56b0883913c9fc4c22a1d293,
     groupAssigned: 'No',
     groupName: 'ios' } },{ '0': 
   { _id: 56b0884213c9fc4c22a1d294,
     groupAssigned: 'No',
     groupName: 'dotNet' } },{ '0': 
   { _id: 56b0885913c9fc4c22a1d295,
     groupAssigned: 'No',
     groupName: 'testing' } },{ '0': 
   { _id: 56b0886213c9fc4c22a1d296,
     groupAssigned: 'No',
     groupName: 'design' } }

在上面的输出中我只实现了元素数组,但我想要用户名,现在用户名是未定义的。怎么做?

2 个答案:

答案 0 :(得分:1)

用户名未显示,因为您未在选择条件中包含该用户名:

user.find().select('username userGroup').exec...应该可以正常使用

查询文档:http://mongoosejs.com/docs/queries.html

答案 1 :(得分:0)

user.find({}, 'username userGroup', function(err, docs) {
  if(err) console.log(err);
  console.log(docs);
});

使用mongoose find方法选择性检索字段。试试它是否适合你。