mongoDB==根据条件来查询文档

//插入数据库模块

const mongoose = require('mongoose');

 

//连接数据库

mongoose.connect('mongodb://localhost/playground', { useNewUrlParse: true })

    .then(result => {

        console.log('数据库连接成功');

    })

    .catch(err => {

        console.log(err, '数据库连接失败');

    })

 

//创建集合规则

const userSchema = new mongoose.Schema({

    name: String,

    age: Number,

    email: String,

    password: String,

    hobbies: [String]

});

 

//使用规则创建集合

const User = mongoose.model('User', userSchema);

 

//查询用户集合中的所有文档

// User.find().then(result => console.log(result));

 

//通过_id字段查找文档

// User.findOne({ _id: '5c09f1e5aeb04b22f8460965' }).then(result => {    console.log(result);})

 

//查找大于15,小于50的文档

//  User.find({ age: { $gt: 15, $lt: 50 } }).then(result => {   console.log(result);}) 

 

//查找包含足球的文档

// User.find({ hobbies: { $in: ['足球'] } }).then(result => { console.log(result) })

 

//选择要查询的字段,如果不需要查询_id,在前面加个-

//  User.find().select('name email').then(result => {    console.log(result);});

 

//根据年龄字段进行升序排列

// User.find().sort('age').then(result => console.log(result));

 

//根据年龄字段进行降序排列

// User.find().sort('-age').then(result => console.log(result));

 

//skip跳过多少条数据 limit限制查询数量

User.find().skip(2).limit(2).then(result => console.log(result));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值