How to fetch data using query in field which contains " : "

I am trying to fetch data in field which contains expression " : ", but i am getting error of Unexpected token :

For eg:
My collection contains 2 documents like-

ObjectId(1)                123
id                               1
name                         shubham
CreateDate                10/11/12

ObjetId(2)                 456
id                              2
name                        anant
CreateDate:             10/11/12

while fetching data using qurey -

var cursor =db.getCollection('dummydata').find({name : {$in : 
[/shubham/i,/anant/i]}})
while (cursor.hasNext()) {
var record = cursor.next();
print(record.id +'|'+ record.name + '|' + record.CreateDate)
} 

I am getting this output-

1|shubham|10/11/12
2|anant|undefined

even though there is CreatDate field in id =2,It is showing undefined,
Please let me know how to troubleshoot this or some other query to fetch data which include" : " expression in the field.

If I were you I would correct my data first. I do not think it is a good idea to have a field named “CreateDate” in one document while it is named “CreateDate:” in another one. Especially if you want to loop and manipulate your data in a consistent way.

2 Likes

try

print(record["id"] +'|'+ record["name"] + '|' + (record["CreateDate"] || record["CreateDate:"]))
1 Like

Thank you for your reply. My approach was aslo same as yours.But sometime we are bound to not use update query …
but anyways thanks…I got result with another approach,…

Hi Thank you,
This worked fine…

1 Like