0

说我有这个结构

{
    "_id" : "4klhrj5hZ",
    "name" : "asdf",
    "startTime" : ISODate("2016-09-20T22:22:08.082Z"),
    "columns" : [
        {
            "_id" : ObjectId("57e1b69087ceb4392ebdf7f4"),
            "createdAt" : ISODate("2016-09-20T22:22:08.088Z"),
            "rows" : [
                {
                    "value" : "adf",
                    "_id" : ObjectId("57e1b7867598bd39a72876ef")
                }
            ]
        },
        {
            "_id" : ObjectId("57e1b69087ceb4392ebdf7f3"),
            "createdAt" : ISODate("2016-09-20T22:22:08.088Z"),
            "rows" : [
                {
                    "value" : "we",
                    "_id" : ObjectId("57e1b69287ceb4392ebdf7f5"),
            ]
        },
        {
            "_id" : ObjectId("57e1b69087ceb4392ebdf7f2"),
            "createdAt" : ISODate("2016-09-20T22:22:08.086Z"),
            "rows" : [
                {
                    "value" : "asdf",
                    "_id" : ObjectId("57e1b7be7598bd39a72876f0")
                }
            ]
        }
    ]
}

我首先有一个列数组,然后是每列的行数组。一个数组数组...我正在尝试编写一个计数查询来告诉我有多少顶级文档包含所有空数组数组。所以在这个例子中,columns[0-2].rows.length === 0, not columns 可以是任何长度。从我见过的例子中最让我绊倒的事情是动态地做嵌套数组而不是像这样引用它

columns.0.rows

谢谢!

编辑:澄清 这是帮助澄清的猫鼬模式

var RowSchema = new Schema({
  value: String,
  createdAt:{
    type: Date,
    'default': Date.now
  }
});

var ColumnSchema = new Schema({
  rows: [RowSchema],
  createdAt:{
    type: Date,
    'default': Date.now
  }
});

var ItemSchema = new Schema({
  _id: {
    type: String,
    unique: true,
    'default': shortid.generate
  },
  name: String,
  columns: [ColumnSchema],
  createdAt:{
    type: Date,
    'default': Date.now
  }
})

我想运行一个查询来查找所有列中包含零行的所有'Item s 。所以我知道如何找到一个空的数组:

Item.find({ columns: { $exists: true, $eq: [] } })

但我想要类似的东西

Item.find({ 'columns.rows': { $exists: true, $eq: [] } })

很抱歉解释不清楚,有时你会忘记设置正确的上下文。谢谢。

4

0 回答 0