Node.js - MongoDB 查询



在 Node.js 的 mongodb 驱动程序模块中定义的 find() 和 findOne() 方法返回满足查询参数的所有文档或指定集合中的第一个文档。您可以使用逻辑运算符在查询对象中构造过滤器,如下所示 -

MongoDB 运算符

MongoDB不使用传统的逻辑运算符符号。取而代之的是,它有自己的运算符,如下所示 -

运算符 描述
$eq

等于 (==)

$gt

大于 (>)

$gte

大于 (>)

$in

如果等于数组中的任何值

$lt

小于 (<)

$lte

小于或等于 (<=)

$ne

不等于 (!=)

$nin

如果不等于数组中的任何值

运算符在 find() 方法中用于应用过滤器。以下语句返回价格为>10000 的产品


const {MongoClient} = require('mongodb');

async function main(){

	 	const uri = "mongodb://localhost:27017/";
	 	const client = new MongoClient(uri);

	 	try {
	 	 	 // Connect to the MongoDB cluster
	 	 	 await client.connect();

	 	 	 // Make the appropriate DB calls

	 	 	 // Create a single new listing
	 	 	 await fetchdocs(client, "mydb", "products");
	 	 	 	
	 	} finally {
	 	 	 // Close the connection to the MongoDB cluster
	 	 	 await client.close();
	 	}
}

main().catch(console.error);


async function fetchdocs(client, dbname, colname){
	 	const result = await client.db(dbname).collection(colname).find({"price":{$gt:10000}}).toArray();
	 	console.log(JSON.stringify(result));
}

输出

[{"_id":"6580964f20f979d2e9a72ae7","ProductID":1,"Name":"Laptop","price":25000},
{"_id":"6580964f20f979d2e9a72ae8","ProductID":2,"Name":"TV","price":40000}]

$and 运算符和 $or运算符可用于复合逻辑表达式。它们的用法如下 -


db.collection.find($and:[{"key1":"value1"}, {"key2":"value2"}])

使用以下命令获取价格在 1000 到 10000 之间的产品。


async function fetchdocs(client, dbname, colname){
	 	const result = await client.db(dbname).collection(colname).find({$and:[{"price":{$gt:1000}}, {"price":{$lt:10000}}]}).toArray();
	 	console.log(JSON.stringify(result));
}

输出

[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000},
{"_id":"6580964f20f979d2e9a72aea","ProductID":4,"Name":"Scanner","price":5000},
{"_id":"6580964f20f979d2e9a72aeb","ProductID":5,"Name":"Printer","price":9000}]

正则表达式

您还可以通过形成正则表达式来创建过滤器。$regex 变量在查询 JSON 表示形式中用作键。以下代码返回名称以 P 开头的所有产品。


async function fetchdocs(client, dbname, colname){
	 	const result = await client.db(dbname).collection(colname).find({Name:{$regex:"^P"}}).toArray();
	 	console.log(JSON.stringify(result));
}

输出

[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000},
{"_id":"6580964f20f979d2e9a72aea","ProductID":4,"Name":"Scanner","price":5000},
{"_id":"6580964f20f979d2e9a72aeb","ProductID":5,"Name":"Printer","price":9000}]

在以下示例中,结果集包含名称以 Ro 开头的文档。


async function fetchdocs(client, dbname, colname){
	 	const result = await client.db(dbname).collection(colname).find({Name: /Ro/}).toArray();
	 	console.log(JSON.stringify(result));
}

输出

[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000}]

要获取名称以 er 结尾的产品,请使用末尾的 $ 符号。


async function fetchdocs(client, dbname, colname){
	 	const result = await client.db(dbname).collection(colname).find({Name: /er$/}).toArray();
	 	console.log(JSON.stringify(result));
}

输出

[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000},
{"_id":"6580964f20f979d2e9a72aea","ProductID":4,"Name":"Scanner","price":5000},
{"_id":"6580964f20f979d2e9a72aeb","ProductID":5,"Name":"Printer","price":9000}]