node.js mongodb find篇

不BB直接上代碼

// 導入mongodb
const mongoose = require("mongoose"); 
//  鏈接數(shù)據(jù)庫
mongoose.connect("mongodb://localhost/playground", { useNewUrlParser: true })
  .then(() => console.log("飛起來了"))
  .catch((err) => console.log(err, "錯了"));
// 接下來 創(chuàng)建集合規(guī)則
const courseScheme = new mongoose.Schema({
  name: String,
  author: String,
  isPublish: Boolean,
  age: Number,
});
// 重點來了
const Data = mongoose.model("Data", courseScheme);
// find sort讓數(shù)據(jù)進行排序 ('age')  我這寫的是年齡的字段  我是通過年齡來排序的
Data.find().sort("age").then((res) => {console.log(res)});
// ("age") 前邊加-號是讓查詢結果為降序
Data.find().sort("-age").then((res) => {console.log(res)});
// skip跳過多少條數(shù)據(jù)  limit限制查詢數(shù)量(limit)可以用來作分頁查詢
Data.find().skip(2).limit(5).then((res) => {console.log(res)});
// 匹配年齡大于20 小于50的   $gt表示大于 $it表示小于
Data.find({age:{$gt:20,$lt:50}) => {console.log(res)});
// 匹配含有“敲代碼”的字段   $in
Data.find(name:{$in:['敲代碼'}).then((res) => {console.log(res)});
// 選擇要查詢的字段   seleat()
Data.find().seleat('name age).then((res) => {console.log(res)});

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容