1

The project i m working on uses mongodb as DB and has a mock data generator. when data is generated via mock data generator it creates objects with string ids into db. And our backend application Spring boot with spring data mongodb stores new objects with _id being ObjectId

{
    "_id" : ObjectId("6163686f6c64722d30303031"),
    "email" : "Price36@hotmail.com",
    "modeOfContact" : "EMAIL",
    "name" : "Leonardo Walter",
    "phone" : "08802273531"
}

{
    "_id" : "customer-1",
    "email" : "Casimer_Jakubowski@hotmail.com",
    "modeOfContact" : "EMAIL",
    "name" : "Kennedy Kilback",
    "phone" : "07624333004"
}

now we have a node based application for fetch calls only that uses "mongodb" package to resolve queries.

when i do following query for objects with String _id it works

findDocument(COLLECTION_NAME, { _id: args.id})

but the query fails when objects have _id as ObjectId

is there a way where i can search for objects by providing _id and data type as well
like

findDocument(COLLECTION_NAME, {  _id: args.id , $type: [ 'string', 'ObjectId']})

i know with $type it will return all records with _id's matching that type. just trying to explain what i wanna achieve here.

2 Answers2

1

but the query fails when objects have _id as ObjectId

To query an ObjectId in mongo from nodejs you need to create an ObjectId:

var ObjectID = require('mongodb').ObjectID;

// Create a new ObjectID (hex string is 24 characters long)
var objectId = new ObjectID('6163686f6c64722d30303031');

// NOTE: you try to run `new ObjectID('customer-1');` you will get an Exception

findDocument(COLLECTION_NAME, { _id: objectId})

I think a better approach would be to update your mock to insert valid ObjectID instead of a string if this doesn't matter to you because you need to guarantee the uniqueness of the _id

Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
  • thanks @Manuel for the response, but whats happening is sometimes i get _id as String and sometimes ObjectId from front end. I tried your solution and it will work when _id coming from frontend is hexadecimal string otherwise it'll throw an exception. – Syed Zishan Aug 13 '20 at 12:28
  • Also i tried to add ObjectId via mock data, in some collections I have references to objects within some objects. In that case in my java side i m getting results only when i query with _id field i.e _id is ObjectId and when i request with reference within my object then i m getting null result.` SomeEntity findById(String id)` returns expected result as _id is ObjectId but `SomeEntity findByReferenceId(String refId)` return null results as ReferenceId is simply hexadecimal string – Syed Zishan Aug 13 '20 at 12:31
  • 1
    If you have mixed format you could just add a `try{}catch{}` when creating a new ObjectID – Manuel Spigolon Aug 13 '20 at 12:31
  • Yeah sure that seems like a quick fix at the moment – Syed Zishan Aug 13 '20 at 12:33
0

the query fails when objects have _id as ObjectId

One way to go about this is using ObjectID construction only when its a valid ObjectId using ObjectID.isValid()

const mongodb = require("mongodb").ObjectID;

const validId = mongodb.ObjectID.isValid(args._id);
/*only convert to ObjectID if validId true*/
const idToQuery = validId ? new mongodb.ObjectID(args._id) : args._id;

findDocument(COLLECTION_NAME, { _id: idToQuery });
ambianBeing
  • 3,449
  • 2
  • 14
  • 25