Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS + MongoDB: Getting data from collection with findOne ()

I have a collection "companies" with several objects. Every object has "_id" parameter. I'm trying to get this parameter from db:

app.get('/companies/:id',function(req,res){
db.collection("companies",function(err,collection){
    console.log(req.params.id);
    collection.findOne({_id: req.params.id},function(err, doc) {
        if (doc){
            console.log(doc._id);
        } else {
            console.log('no data for this company');
        }
    });
});
});

So, I request companies/4fcfd7f246e1464d05000001 (4fcfd7f246e1464d05000001 is _id-parma of a object I need) and findOne returns nothing, that' why console.log('no data for this company'); executes.

I'm absolutely sure that I have an object with _id="4fcfd7f246e1464d05000001". What I'm doing wrong? Thanks!

However, I've just noticed that id is not a typical string field. That's what mViewer shows:

"_id": {
        "$oid": "4fcfd7f246e1464d05000001"
    },

Seems to be strange a bit...

like image 435
f1nn Avatar asked Jun 07 '12 09:06

f1nn


People also ask

What does findOne return MongoDB?

The findOne() method finds and returns one document that matches the given selection criteria. If multiple documents satisfy the given query expression, then this method will return the first document according to the natural order which reflects the order of documents on the disk.

What is the use of findOne in node JS?

The findOne() method uses a query document that you provide to match only the subset of the documents in the collection that match the query. If you don't provide a query document or if you provide an empty document, MongoDB matches all documents in the collection.

Is findOne faster than find in MongoDB?

find() returns a cursor whereas findOne() returns the exact document. It is faster to use find() + limit() because findOne() will always read + return the document if it exists. find() just returns a cursor (or not) and only reads the data if you iterate through the cursor.


2 Answers

You need to construct the ObjectID and not pass it in as a string. Something like this should work:

var BSON = require('mongodb').BSONPure;
var obj_id = BSON.ObjectID.createFromHexString("4fcfd7f246e1464d05000001");

Then, try using that in your find/findOne.

Edit: As pointed out by Ohad in the comments (thanks Ohad!), you can also use:

new require('mongodb').ObjectID(req.params.id)

Instead of createFromHexString as outlined above.

like image 190
Adam Comerford Avatar answered Sep 22 '22 12:09

Adam Comerford


That's because _id field in mongo isn't of string type (as your req.params.id). As suggested in other answers, you should explicitly convert it.

Try mongoskin, you could use it like node-mongodb-native driver, but with some sugar. For example:

// connect easier
var db = require('mongoskin').mongo.db('localhost:27017/testdb?auto_reconnect');

// collections
var companies = db.collection('companies');

// create object IDs
var oid = db.companies.id(req.params.id);

// some nice functions…
companies.findById();

//… and bindings
db.bind('companies', {
  top10: function(callback) {
    this.find({}, {limit: 10, sort: [['rating', -1]]).toArray(callback);
  } 
});

db.companies.top10(printTop10);
like image 36
Aleksei Zabrodskii Avatar answered Sep 22 '22 12:09

Aleksei Zabrodskii