O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Searching for a document in MongoDB with Node.js

Being able to insert documents wouldn't do you much good if you didn't have a way to search for documents. MongoDB lets you specify a template on which to match, and returns objects matching that template.

As with insertions and updates, you'll work with a collection of documents, invoking the collection's find method.

How to do it...

Here's an example that finds all documents in the test collection with a call of kf6gpe-7 and prints them to the console:

var mongo = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/test'; mongo.connect(url, function(error, db) { console.log("mongo.connect returned " + error); var cursor = collection.find({call: 'kf6gpe-7'}); cursor.toArray(function(error, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required