Start a new topic

Whenever I try to query by _id, I get zero results

When querying using Business Logic's collectionAccess module by the _id field, zero results are returned. Yet when I run the same query via the REST API, I get a result back.

2 people have this question
1 Comment

The reason that you cannot find your record is that the _id stored in the database is not a string, but rather a BSON object. The query in the REST API automatically does this conversion, but the collectionAccess module in business logic gives you raw access to the database. We've provided a helper function in collectionAccess, objectID, to convert a string to a BSON object. So in order to run your query, you should first convert it using the `objectID` method.



var bsonID = collectionAccess.objectID(request.body._id);



For example:



function onRequest(request, response, modules){

var collectionAccess = modules.collectionAccess;

var myID = collectionAccess.objectID("51756c8f3e3b484004023792");

var myX = collectionAccess.collection("x");

var logger = modules.logger.info;

var callback = function(err, result) {

response.body = result;

response.complete(200);

}

myX.find({"_id": myID},callback);

}


2 people like this
Login or Signup to post a comment