Mongodb : How to find documents in which fields match an ObjectId or a string? -


i have documents in collection in mongodb :

{_id : objectid('533af69b923967ac1801e113'), fkey : '533aeb09ebef89282c6cc478', ... } {_id : objectid('5343bd1e2305566008434afc'), fkey : objectid('5343bd1e2305566008434afc'), ...} } 

as can see field fkey can set string or objectid.

i documents match '533aeb09ebef89282c6cc478' or objectid('5343bd1e2305566008434afc').

but if run :

db.mycollection.find({fkey : '533aeb09ebef89282c6cc478'}) 

i first document of collection.

is there way configure mongodb in order documents match request without checking type ?

thanks help.

pierre

there 2 options here.

you use mongo's $or operator:

db.mycollection.find({    $or: [      { fkey: '533aeb09ebef89282c6cc478' },      { fkey: objectid( '533aeb09ebef89282c6cc478' ) }    ]  }) 

the $or operator performs logical or operation on array of 2 or more <expressions> , selects documents satisfy @ least 1 of <expressions>.


you use $in operator:

db.mycollection.find({    fkey: {      "$in": [ '533aeb09ebef89282c6cc478', objectid( '533aeb09ebef89282c6cc478' )  ]    } }) 

the $in operator selects documents value of field equals value in specified array.


it sounds me these inconsistencies not meant there. recommend going through code , data pipelines , figure out who/what inserting fkey value unknown datatype.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -