Node.js MongoDB 查找
在 MongoDB 中,我们使用 find 和 findOne 方法在集合中查找数据。
就像 SELECT 语句用于在 MySQL 数据库的表中查找数据一样。
查找一个
要从 MongoDB 中的集合中选择数据,可以使用 findOne() 方法。
findOne() 方法返回选择中的第一个匹配项。 findOne() 方法的第一个参数是查询对象。在本例中,我们使用一个空查询对象,它选择集合中的所有文档(但只返回第一个文档)。
实例
查找 "customers" 集合中的第一个文档:
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");dbo.collection("customers").findOne({}, function(err, result) {if (err) throw err;console.log(result.name);db.close();});});
将上述代码保存在名为 "demo_mongodb_findone.js" 的文件中,然后运行该文件:
运行 "demo_mongodb_findone.js"
结果如下:
Company Inc.
查找全部
要从 MongoDB 中的表中选择数据,还可以使用 find() 方法。find() 方法返回所选内容中出现的所有内容。
find() 方法的第一个参数是查询对象。在本例中,我们使用一个空查询对象,它选择集合中的所有文档。
find() 方法中没有参数提供与MySQL中的SELECT*相同的结果。
实例
查找 "customers" 集合中的所有文档:
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");dbo.collection("customers").find({}).toArray(function(err, result) {if (err) throw err;console.log(result);db.close();});});
将上述代码保存在名为 "demo_mongodb_find.js" 的文件中,然后运行该文件
运行 "demo_mongodb_find.js"
结果如下:
[{ _id: 58fdbf5c0ef8a50b4cdd9a84 , name: 'John', address: 'Highway 71'},{ _id: 58fdbf5c0ef8a50b4cdd9a85 , name: 'Peter', address: 'Lowstreet 4'},{ _id: 58fdbf5c0ef8a50b4cdd9a86 , name: 'Amy', address: 'Apple st 652'},{ _id: 58fdbf5c0ef8a50b4cdd9a87 , name: 'Hannah', address: 'Mountain 21'},{ _id: 58fdbf5c0ef8a50b4cdd9a88 , name: 'Michael', address: 'Valley 345'},{ _id: 58fdbf5c0ef8a50b4cdd9a89 , name: 'Sandy', address: 'Ocean blvd 2'},{ _id: 58fdbf5c0ef8a50b4cdd9a8a , name: 'Betty', address: 'Green Grass 1'},{ _id: 58fdbf5c0ef8a50b4cdd9a8b , name: 'Richard', address: 'Sky st 331'},{ _id: 58fdbf5c0ef8a50b4cdd9a8c , name: 'Susan', address: 'One way 98'},{ _id: 58fdbf5c0ef8a50b4cdd9a8d , name: 'Vicky', address: 'Yellow Garden 2'},{ _id: 58fdbf5c0ef8a50b4cdd9a8e , name: 'Ben', address: 'Park Lane 38'},{ _id: 58fdbf5c0ef8a50b4cdd9a8f , name: 'William', address: 'Central st 954'},{ _id: 58fdbf5c0ef8a50b4cdd9a90 , name: 'Chuck', address: 'Main Road 989'},{ _id: 58fdbf5c0ef8a50b4cdd9a91 , name: 'Viola', address: 'Sideway 1633'}]
查询指定字段
find() 方法的第二个参数是 projection 对象,它描述了结果中要包含的字段。
实例
返回 customers 集合中所有文档的 "name" 和 "address"字段:
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");dbo.collection("customers").find({},<strong> { projection: { _id: 0, name: 1, address: 1 } }</strong>).toArray(function(err, result) {if (err) throw err;console.log(result);db.close();});});
将上述代码保存在名为 "demo_mongodb_find_fields.js" 的文件中,然后运行该文件:
运行 "demo_mongodb_find_fields.js"
结果如下:
[{ name: 'John', address: 'Highway 71'},{ name: 'Peter', address: 'Lowstreet 4'},{ name: 'Amy', address: 'Apple st 652'},{ name: 'Hannah', address: 'Mountain 21'},{ name: 'Michael', address: 'Valley 345'},{ name: 'Sandy', address: 'Ocean blvd 2'},{ name: 'Betty', address: 'Green Grass 1'},{ name: 'Richard', address: 'Sky st 331'},{ name: 'Susan', address: 'One way 98'},{ name: 'Vicky', address: 'Yellow Garden 2'},{ name: 'Ben', address: 'Park Lane 38'},{ name: 'William', address: 'Central st 954'},{ name: 'Chuck', address: 'Main Road 989'},{ name: 'Viola', address: 'Sideway 1633'}]
不允许在同一对象中同时指定 0 和 1 值(除非其中一个字段是 _id 字段)。如果指定值为 0 的字段,则所有其他字段的值均为 1,反之亦然:
实例
此示例将从结果中排除 "address":
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");dbo.collection("customers").find({},<strong> { projection: { address: 0 } }</strong>).toArray(function(err, result) {if (err) throw err;console.log(result);db.close();});});
要排除 _id 字段,必须将其值设置为 0:
实例
此示例将仅返回 "name" 字段:
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");dbo.collection("customers").find({},<strong> { projection: { _id: 0, name: 1 } }</strong>).toArray(function(err, result) {if (err) throw err;console.log(result);db.close();});});
实例
此示例将给出与第一个示例相同的结果;返回除 _id 字段之外的所有字段:
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");dbo.collection("customers").find({},<strong> { projection: { _id: 0 } }</strong>).toArray(function(err, result) {if (err) throw err;console.log(result);db.close();});});
实例
如果在同一对象中同时指定 0 和 1 值,则会出现错误(除非其中一个字段是 _id 字段):
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");dbo.collection("customers").find({},<strong> { projection: { name: 1, address: 0 } }</strong>).toArray(function(err, result) {if (err) throw err;console.log(result);db.close();});});
结果对象
从上面示例的结果可以看出,可以将结果转换为包含每个文档作为对象的数组。
要返回第三个文档的地址,只需参考第三个数组对象的 address 属性:
实例
返回第三个文档的 address 属性:
结果如下:
Apple st 652