【node】------mongoose的基本使用------【巷子】


1、安裝mongoose

npm install mongoose

 

2、啟動數據庫

mongod --dbpath d:\data\db

 

3、引入mongoose模塊並連接數據庫

const mongoose = require("mongoose");

mongoose.connect("mongodb://127.0.0.1:27017/test1",function(err) {
     if(err){
         console.log('連接失敗');
     }else{
         console.log("連接成功")
     }
});

 

 

 4、創建表以及字段類型

const User = mongoose.model("user",{
    name:String,
    age:Number
})

 5、增

const user = new User({
    name:"張三",
    age:19
})

user.save().then((result)=>{
    console.log("成功的回調")
},()=>{
    console.log("失敗的回調")
})

 6、刪

1、刪除指定數據
User.remove({name:"zhao"}).then((result)=>{
    console.log(result)
})

result:是一個對象 返回值是受影響條數

2、刪除所有數據 User.remove({}).then((result)=>{ console.log(result) })

//刪除指定ID
3、User.findByIdAndRemove(id值).then((result)=>{

})

 7、改

User.update({name:"ya"},{$set:{name:"hua"}},{multi:true}).then((result)=>{
             console.log(result)
})

multi:true  表示修改多條數據

User.findByIdAndUpdate(id值,{$set:{需要修改的內容}}.then((result)=>{})

 

8、查

001查詢符合條件的所有數據

User.find({name:ya}).then((result)=>{
    console.log(result)
})

result是查到的數據

 002、查詢所有數據

User.find().then((result)=>{
             console.log(result)
})

 003、查詢單條數據

User.findOne({name:"zhao"}).then((result)=>{
             console.log(result);
 })

 004、條件查詢:

$lt(小於) $lte(小於等於) $gt(大於) $gte(大於等於) $ne(不等於);

User.find({"age":{"$lt":20}}).then((result)=>{
             console.log(result);
 })

User.find({"age":{"$lte":20}}).then((result)=>{
             console.log(result);
 })

 User.find({"age":{"$gt":20}}).then((result)=>{
             console.log(result)
})

User.find({"age":{"$gte":20}}).then((result)=>{
             console.log(result)
})

User.find({"age":{"$ne":19}}).then((result)=>{
             console.log(result)
})

 005、$in(包含 等於)  $nin(不包含 不等於)

User.find({"age":{"$in":[18,19]}}).then((result)=>{
             console.log(result)
 })

User.find({"age":{"$nin":[18,19]}}).then((result)=>{
             console.log(result)
})

 006、$or(或)

User.find({"$or":[{name:"zhao"},{age:20}]}).then((result)=>{
             console.log(result)
})

 007、$exists (判斷當前關鍵字是否存在)

User.find({name:{"$exists":true}}).then((result)=>{
             console.log(result);
})

 008、查詢指定列 如果不想要id值 只需要設置_id:0

User.find({},{name:1,age:1,_id:0}).then((result)=>{
             console.log(result);
})

 009、升序降序 sort()

User.find().sort({age:1}).then((result)=>{
             console.log(result)
})

 010、模糊查詢 //

User.find({name:/a/}).then((result)=>{
             console.log(result)
})

User.find({name:/^z/}).then((result)=>{
             console.log(result);
})

User.find({name:/z$/}).then((result)=>{
             console.log(result);
})

 011、skip(n):查詢n條以后的數據

User.find().skip(3).then((result)=>{
             console.log(result);
})

 012、顯示n-m之間的數據 skip:跳過n條 limit 顯示m-n條

User.find().skip(3).limit(2).then((result)=>{
             console.log(result)
})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM