如何$ unwind并包含原始数组的索引

时间:2016-04-16 18:33:55

标签: arrays mongodb

我正在尝试以下方法:

db.c1.aggregate([{$unwind: "$arr"}])

这很好用。但是,我想要包含数组的索引并尝试following the docs like so

> db.c1.aggregate( [ { $unwind: { path: "$arr", includeArrayIndex: "arrayIndex" } } ] )

这会产生以下错误:

assert: command failed: {
        "errmsg" : "exception: the $unwind field path must be specified as a string",
        "code" : 15981,
        "ok" : 0
} : aggregate failed
Error: command failed: {
        "errmsg" : "exception: the $unwind field path must be specified as a string",
        "code" : 15981,
        "ok" : 0
} : aggregate failed
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
    at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
    at (shell):1:7
2016-04-16T18:37:38.100+0000 E QUERY    Error: command failed: {
        "errmsg" : "exception: the $unwind field path must be specified as a string",
        "code" : 15981,
        "ok" : 0
} : aggregate failed
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
    at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
    at (shell):1:7 at src/mongo/shell/assert.js:13

我认为问题可能是"$arr"所以我在"arr"中替换了,但这会导致同样的错误。

我的查询有什么问题?为了记录,这是我试图使用的文件:

{ "_id" : ObjectId("57127e0475026f0d7a8339c9"), "arr" : [ { "val" : 5, "time" : ISODate("2016-04-16T18:01:40.833Z") }, { "val" : 5, "time" : ISODate("2016-04-16T18:03:52.503Z") }, [ { "val" : 99, "time" : ISODate("2016-04-16T18:04:17.637Z") } ] ] }

0 个答案:

没有答案