- Node &; MongoDB 教程
- Node &; MongoDB - 主页
- Node &; MongoDB - 概述
- Node &; MongoDB - 环境设置
- Node &; MongoDB 范例
- Node &; MongoDB - 连接数据库
- Node &; MongoDB - 显示数据库
- Node &; MongoDB - 删除数据库
- Node &; MongoDB - 创建集合
- Node &; MongoDB - 删除集合
- Node &; MongoDB - 显示集合
- Node &; MongoDB - 插入文档
- Node &; MongoDB - 选择文档
- Node &; MongoDB - 更新文档
- Node &; MongoDB - 删除文档
- Node &; MongoDB - 嵌入文档
- Node &; MongoDB - 限制记录
- Node &; MongoDB - 排序记录
- Node &; MongoDB 实用资源
- Node &; MongoDB - 快速指南
- Node &; MongoDB - 实用资源
- Node &; MongoDB - 讨论
Node &; MongoDB - 限制记录
要限制集合中选择记录的数量,可以使用 collection.find().limit() 方法来选择所需的文档。
database.collection("sampleCollection").find({}).limit(2).toArray(function(error, result) {
if (error) throw error;
console.log(result);
});
范例
试试以下范例,在 mongodb 集合中选择有限的文档 −
将以下范例复制粘贴为 mongodb_example.js 文件 −
const MongoClient = require('mongodb').MongoClient;
// Prepare URL
const url = "mongodb://:27017/";
// make a connection to the database
MongoClient.connect(url, function(error, client) {
if (error) throw error;
console.log("Connected!");
// Connect to the database
const database = client.db('myDb'); database.collection("sampleCollection").find({}).limit(2).toArray(function(error, result) {
if (error) throw error;
console.log(result);
});
// close the connection
client.close();
});
输出
使用 node 执行 mysql_example.js 脚本,并验证输出。
node mongodb_example.js
Connected!
[
{
_id: 60c4bbb40f8c3920a0e30fdd,
First_Name: 'Radhika',
Last_Name: 'Sharma',
Date_Of_Birth: '1995-09-26',
e_mail: 'radhika_sharma.123@gmail.com',
phone: '9000012345'
},
{
_id: 60c4bbb40f8c3920a0e30fde,
First_Name: 'Rachel',
Last_Name: 'Christopher',
Date_Of_Birth: '1990-02-16',
e_mail: 'rachel_christopher.123@gmail.com',
phone: '9000054321'
}
]
广告