1

Let's assume we have a query builder service B that spits out mongo db query when called. This query is received by service A and it executes it as is with mongo db official nodejs driver.

How do I send something like :

[{
  _id: new mongo.ObjectID("5f3258cfbaaccedaa5dd2d96"),
  phone: "666"
}, {
  _id: new mongo.ObjectID("5f3258cfbaaccedaa5dd2da2"),
  phone: "555"
}]

from service B to service A?

EDIT:

The following works perfectly fine:

var q = { _id: new mongo.ObjectID("5f3258cfbaaccedaa5dd2d96") };
const result = await this.db.collection("persons").find(q).toArray();

The following doesn't work:

var q = { _id: { $oid: "5f3258cfbaaccedaa5dd2d96" } }
const result = await this.db.collection("persons").find(q).toArray();

Now,

var q = { _id: new mongo.ObjectID("5f3258cfbaaccedaa5dd2d96") };
JSON.stringify(q)

gives you : {"_id":"5f3258cfbaaccedaa5dd2d96"} and if you pass this to service A. You can not use it in service A as follows:

  const result = await this.db.collection("persons").find(qStr).toArray();

Or as,

  const result = await this.db.collection("persons").find(JSON.parse(qStr)).toArray();
3
  • what is the issue when var q = { _id: new mongo.ObjectID("5f3258cfbaaccedaa5dd2d96") }; const result = await this.db.collection("persons").find(q).toArray(); is used? Commented Aug 28, 2020 at 15:57
  • No issue at all. It works perfectly fine in service B. But the question is how do you send this to service A and execute it in service A? @whoami Commented Aug 28, 2020 at 15:59
  • Yes I mean what's happening when you send it to service A ? Commented Aug 28, 2020 at 16:23

2 Answers 2

2

You need to:

  • Serialize your documents to extended json on one end
  • Deserialize your documents from extended json to language-native data structures on the other end

See https://github.com/mongodb/js-bson#node-no-bundling for how to serialize and deserialize.

You cannot feed extended json-type annotated hashes to driver functions that expect native types (which are all of them, basically, other than the one that specifically parses extended json), like you tried to do.

var q = { _id: new mongo.ObjectID("5f3258cfbaaccedaa5dd2d96") };
const serializedQ = BSON.serialize(q);
const deserializedQ = BSON.deserialize(serializedQ);    
const result = await this.db.collection("persons").find(deserializedQ).toArray();
Sign up to request clarification or add additional context in comments.

3 Comments

yep, that's exactly what I needed. Thank you.
However I do have a question. BSON.serialize(q) does not give you a string. So is it possible to send it to another service in HTTP call?
BSON is a binary format, you should be getting a binary string after serializing to it.
2

There is a standard that MongoDB calls "Extended JSON" that defines how you can encode all BSON data types in regular JSON.

It will become something like

{ _id : {$oid: "5f3258cfbaaccedaa5dd2d96"} }

Most MongoDB tools will be able to convert to and from that format.

5 Comments

I thought so too. But the following query fails to execute: const result = await this.db.collection("persons").bulkWrite([{ _id: {"$oid":"5f3258cfbaaccedaa5dd2d96"} , phone: "666" } , { _id: {"$oid":"5d505646cf6d4fe581014ab2"} , phone: "555" }].map(function (p) { return { updateOne: { filter: { _id: p._id }, update: { $set: { phone: p.phone } } } } }))
and if you mean that { _id : {$oid: "5f3258cfbaaccedaa5dd2d96"} } is to be converted to new mongo.ObjectID("5f3258cfbaaccedaa5dd2d96"), is there a programmatic way of doing that? like BSON.parse({$oid: "5f3258cfbaaccedaa5dd2d96"})
How do I convert [{"_id":"5f3258cfbaaccedaa5dd2d96","phone":"666"},{"_id":"5f3258cfbaaccedaa5dd2da2","phone":"555"}]; into { _id: new mongo.ObjectID("5f3258cfbaaccedaa5dd2d96"), phone: "232" } and vice verse ?
@SamuraiJack: no issue with this answer! So let's say data is transferred between two systems in the form of JSON specific JSON string as ObjectId() is not of JSON type, this query can be converted to JSON string transferred to other service where it can be JSON.parse() and executed on DB as is where I believe DB will convert string to ObjectId() but if you wanted this conversion to happen in code then service B will do new mongo.ObjectID("") but in transfer ObjectId() will be converted to string, again in service A needs to convert string to objectId
@whoami please see my edit. I have tried to explain myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.