当前位置: 动力学知识库 > 问答 > 编程问答 >

mongodb - How to $unwind and include index from original array

问题描述:

I am trying the following:

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

And this works fine. However, I want to include the index of the array and tried following the docs like so:

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

That produces the following error:

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

I thought perhaps the problem was "$arr" so I substituted in "arr" but this results in the same error.

What is wrong in my query? For the record, here's the document I am trying to use:

{ "_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") } ] ] }

分享给朋友:
您可能感兴趣的文章:
随机阅读: