find all entries having type_id:ObjectId('random id')

Viewed 43

I am trying to repair a mongodb database where in some of the type_id ObjectId got inserted i tried to use regex but this is not working

find({"type_id":{$regex:/ObjectId\(.*\)/}})

Any Suggestion?

1 Answers

Simple version:

db.foo.find({"type_id": {$type: "objectId"}});

Using the agg pipeline. I prefer pipelines because as your expression complexity grows, you don't get bound into the older, smaller feature set of find:

db.foo.aggregate([
    { $match: {$expr: {$eq: [{$type:"$type_id"}, "objectId"]} }}
]);

Look at https://docs.mongodb.com/manual/reference/operator/query/type to find the string values returned by $type.

Related