demo_mongodb_query_s.js:
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; MongoClient.connect(url, function(err, db) { if (err) throw err; var dbo = db.db("mydb"); /*Return only the documents where the address starts with an "S":*/ var query = { address: /^S/ }; dbo.collection("customers").find(query).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
C:\Users\My Name>node demo_mongodb_query_s.js
[
{ _id: 58fdbf5c0ef8a50b4cdd9a8b , name: 'Richard', address: 'Sky st 331' },
{ _id: 58fdbf5c0ef8a50b4cdd9a91 , name: 'Viola', address: 'Sideway 1633' }
]