
- 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' } ]
廣告