Mongolian DeadBeef.toArray () returns _id in unexpected format

I love the Maygogol dead man, but I'm at a dead end. I would like to get the results of a simple .find () to return in the same JSON format that matches the Mongo command line output:

$ db.mycollection.find(); # outputs.. # { ...some data... , "_id" : ObjectId("4f0b371c0000008b6d000008") } 

However, with deedbeef, the .find () method does not return a result or does not provide a callback. So I used .toArray (); which seems right to me.

 Mongolian = require("mongolian"), server = new Mongolian, db = server.db("mydatabase"), mycollection = db.collection("mycollection"), mycollection.find().toArray(function(err, data){ res.write(JSON.stringify(data)); }); // outputs.. // { ...some data... , _id: { bytes: <Buffer 4f 0b 61 5a 00 00 00 7e 6e 00 00 06> } } 

Dumping the _id binary (I assume this is a buffer) results in the @ # $ metric! tons of data. What is the correct way to return JSON from mycollection.find ()?

~~~~~~~

I was able to remove _id from the results using the following command:

  mycollection.find({}, { id:0 }).toArray(function(err, data){ res.write(JSON.stringify(data)); }); 

However, the big problem associated with converting _id from JSON to BSON remains.

+4
source share
1 answer

The problem is that β€œdata” is an array of document objects, but not all data has a JSON compatible format.

Have a look here: https://github.com/marcello3d/node-mongolian under BSON Data Types.

It looks like your document is of type ObjectId, so you need to delete the ObjectId data before converting to JSON or you need to convert the data to a format that works.

+3
source

Source: https://habr.com/ru/post/1390322/


All Articles