Closed IvanMMM closed 9 years ago
Try using the explicit projection form in mongoose:
TickModel.findOne({
"symbol": 'EURUSD',
"tickData.timestamp": {
"$gte": ISODate("2015-04-17T10:06:00.000Z"),
"$lte": ISODate("2015-04-17T10:10:00.000Z")
},
"MTVersion": 4
}, {
"symbol": 1,
"MTVersion": 1,
"tickData": {
"$elemMatch": {
"timestamp": {
"$gte": ISODate("2015-04-17T10:06:00.000Z"),
"$lte": ISODate("2015-04-17T10:10:00.000Z")
}
}
}
}, callback);
Does that work for you?
Yes, it worked as expected. One parent doc and one subdoc.
This is because mongoose's .elemMatch()
function is a helper for MongoDB's $elemMatch query operator rather than the $elemMatch projection operator. See the mongoose docs on elemMatch. Admittedly the distinction between the two is tricky.
You can either use the explicit form I gave you or the .select()
helper:
TickModel.
findOne({
"symbol": 'EURUSD',
"tickData.timestamp": {
"$gte": ISODate("2015-04-17T10:06:00.000Z"),
"$lte": ISODate("2015-04-17T10:10:00.000Z")
},
"MTVersion": 4
}).
select(
"tickData": {
"$elemMatch": {
"timestamp": {
"$gte": ISODate("2015-04-17T10:06:00.000Z"),
"$lte": ISODate("2015-04-17T10:10:00.000Z")
}
}
}
}).exec(callback);
Hello. I'm using Win7x64, nodejs 12.2, mongo 3.0.2, mongoose 4.0.1.
Here's my request:
This request always returns full list of subdocuments or null.
Using elemMatch also does noting. Parent will full subdoc list or null.
Request from mongo shell returns parent and one subdocument, as expected.
Here's discussion at stackoverflow