Reputation: 313
My mongo documents look like below
{
"_id": ObjectId("59b0ee362976fb61fc54708d"),
"id_str": "29979814",
"eventCat": {
"1": [2],
"2": [10, 5, 2, 2, 10],
"7": [10],
"8": [10, 5]
}
}
I want to check the existence of "eventCat.1" or "eventCat.2" etc. and update something. But my problem is when I refer to the fields from a function it does not work.
db.testCorpus.find({"id_str" : "29979814"}).forEach( function (x) {
tmp=x.eventCat.1;
if(tmp != null) {
print(x.eventCat.1);
}
})
2017-09-20T00:08:53.115+0530 E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:98
If I use quotes it prints the string literal. I tried using $ with and with out quotes but to no avail.
db.testCorpus.find({"id_str" : "29979814"}).forEach( function (x) {
tmp='$x.eventCat.1';
if(tmp != null) {
print(tmp);
}
})
> $x.eventCat.1
I guess this is due to the numeric field name. Because, in other embedded fields I get desired outputs. Is there any way to solve this? Thank you.
Upvotes: 1
Views: 230
Reputation: 5212
You should try this :
db.testCorpus.find({"id_str" : "29979814"}).forEach( function (x) {
tmp=x.eventCat['1'];
if(tmp != null) {
print(x.eventCat['1']);
}
})
> 2
Upvotes: 1