zoukankan      html  css  js  c++  java
  • 【node】mongoose的基本使用

    1、安装mongoose

    npm install mongoose

     

    2、启动数据库

    mongod --dbpath d:datadb

     

    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)
    })

  • 相关阅读:
    ArcEngine中删除地物点(C#)
    Siliverlight常识
    ArcEngine 中打开数据源的连接 AO学习资料笔记
    第一次执行时没有问题,重复执行会出错、GP循环
    理解 ArcObjects 中的游标
    AO 中关于坐标系统的感想 AO学习资料 阅读
    http://www.chinabzw.com/bzlist/7_1.htm
    Delphi 文件操作(4)Reset
    c 语言指针
    Delphi 获取星期几
  • 原文地址:https://www.cnblogs.com/donve/p/10265087.html
Copyright © 2011-2022 走看看