Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

mongoose 嵌套查找匹配问题

$
0
0

先上 格式

var UserSchema = new mongoose.Schema({
  name: {type: String, trim: true, require: true},
  username: {type: String, unique: true, require: true, trim: true},
  password: {type: String, require: true},
  admin: {type: Boolean, default: false},
  date: {type: Date, default: Date.now},
  money: {type: Number, default: 0},
  accounts: [{
    status: {type: Number, require: true},
    date: {type: Date, require: true},
    thing: {type: String, require: true},
    money: {type: Number, require: true}
  }]
})

我存储的数据:

{
    "_id" : ObjectId("58e086623bbf960351ac71d9"),
    "name" : "xiaoming",
    "username" : "xiao",
    "password" : "ming",
    "accounts" : [ 
        {
            "status" : 1,
            "date" : ISODate("2017-03-23T02:30:24.837Z"),
            "thing" : "play",
            "money" : 10,
            "_id" : ObjectId("58e086623bbf960351ac71e2")
        }, 
        {
            "status" : 1,
            "date" : ISODate("2017-03-24T02:30:24.837Z"),
            "thing" : "sleep",
            "money" : 20,
            "_id" : ObjectId("58e086623bbf960351ac71e1")
        }, 
        {
            "status" : 1,
            "date" : ISODate("2017-03-25T02:30:24.837Z"),
            "thing" : "play",
            "money" : 10,
            "_id" : ObjectId("58e086623bbf960351ac71e0")
        }, 
        {
            "status" : 2,
            "date" : ISODate("2017-03-23T02:30:24.837Z"),
            "thing" : "play",
            "money" : 10,
            "_id" : ObjectId("58e086623bbf960351ac71df")
        }, 
        {
            "status" : 2,
            "date" : ISODate("2017-03-24T02:30:24.837Z"),
            "thing" : "sleep",
            "money" : 20,
            "_id" : ObjectId("58e086623bbf960351ac71de")
        }, 
        {
            "status" : 2,
            "date" : ISODate("2017-03-25T02:30:24.837Z"),
            "thing" : "play",
            "money" : 10,
            "_id" : ObjectId("58e086623bbf960351ac71dd")
        }, 
        {
            "status" : 3,
            "date" : ISODate("2017-03-23T02:30:24.837Z"),
            "thing" : "play",
            "money" : 10,
            "_id" : ObjectId("58e086623bbf960351ac71dc")
        }, 
        {
            "status" : 3,
            "date" : ISODate("2017-03-24T02:30:24.837Z"),
            "thing" : "sleep",
            "money" : 20,
            "_id" : ObjectId("58e086623bbf960351ac71db")
        }, 
        {
            "status" : 4,
            "date" : ISODate("2017-03-25T02:30:24.837Z"),
            "thing" : "play",
            "money" : 10,
            "_id" : ObjectId("58e086623bbf960351ac71da")
        }
    ],
    "money" : 0,
    "date" : ISODate("2017-04-02T05:04:34.550Z"),
    "admin" : false,
    "__v" : 0
}

重点 重点 我查询的语句

// var b =await UserModel.aggregate().unwind('accounts').exec()   //这条是正常
  // var b = await UserModel.aggregate().unwind('accounts').match({'accounts.status':'1'}).exec(); //这条匹配到是空的

我要实现的是 查询到accounts.status =1 的所有 子_id 理想是这样子的

// UserModel.aggregate().unwind('accounts').match({'accounts.status':'1'}).sort({'accounts.money':1}).skip(0).limit(10).group({_id:"$_id",accounts:{$push:"$accounts"}}).exec()

Viewing all articles
Browse latest Browse all 14821

Trending Articles