Node.js操作MongoDB查询文档

一、连接数据库

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/zx', { useNewUrlParser: true })
    .then(() => console.log('成功'))
    .catch(err => console.log(err, '失败'));

命令行执行,返回成功则成功连接到数据库。

二、创建集合

创建users集合

const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
    email: String,
    password: String,
    hobbies: [String]
});


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

三、查询文档

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

 users.find().then(result => console.log(result));

2.通过指定的字段查找文档

uesrs.find({_id: '5c09f267aeb04b22f8460968'}).then(result => console.log(result))

3.findOne方法返回一条文档 默认返回当前集合中的第一条文档

users.findOne({ name: '李四' }).then(result => console.log(result))

4、 .find({字段: {$gt: 大于, $lt: 小于}})查询一个区间内的文档

users.find({ age: { $gt: 10, $lt: 30 } }).then(result => console.log(result));

5、 find({字段: {$in: [‘包含的字段’]}})查询包含指定字段的文档

users.find({ hobbies: { $in: ['橄榄球'] } }).then(result => console.log(result))

6、.select(‘字段 -字段’)只查询出制定的字段 加-不查询

 users.find().select('age name -_id').then(result => console.log(result))

7、 .sort(‘字段’)按照升序排列文档 所有的文档 不只是查询的文档

users.find().sort('age').then(result => console.log(result))

8、 字段前加-降序排列

users.find().sort('-age').then(result => console.log(result))

9、.skip(n).limit(m) 查询到的文档 跳过前n条显示前m条

users.find().skip(3).limit(3).then(result => console.log(result))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mingo 是 MongoDB 查询语言的 JavaScript 实现。Mingo 利用 MongoDB 风格查询,在客户端或者服务器端环境下,允许直接查询内存的 JavaScript 对象。特性:Comparisons Operators ($gt, $gte, $lt, $lte, $ne, $nin, $in)Logical Operators ($and, $or, $nor, $not)Evaluation Operators ($regex, $mod, $where)Array Operators ($all, $elemMatch, $size)Element Operators ($exists, $type)Aggregation Pipeline Operators ($group, $match, $project, $sort, $limit, $unwind, $skip)Conditional Operators ($cond, $ifNull)Group Operators ($addToSet, $sum, $max, $min, $avg, $push, $first, $last)Arithmetic Operators ($add, $divide, $mod, $multiply, $subtract)String Operators ($cmp, $strcasecmp, $concat, $substr, $toLower, $toUpper)Set Operators ($setEquals, $setIntersection, $setDifference, $setUnion, $setIsSubset, $anyElementTrue, $allElementsTrue)Projection Operators ($elemMatch, $slice)JSON stream filtering and projection. NodeJS only使用var Mingo = require('mingo'); // or just access *Mingo* global in browser // setup the key field for your collection Mingo.setup({     key: '_id' // default }); // create a query with criteria // find all grades for homework with score >= 50 var query = new Mingo.Query({     type: "homework",     score: { $gte: 50 } });搜索和过滤// filter collection with find() var cursor = query.find(collection); // shorthand with query criteria // cursor = Mingo.find(collection, criteria); // sort, skip and limit by chaining cursor.sort({student_id: 1, score: -1})     .skip(100)     .limit(100); // count matches cursor.count(); // iterate cursor // iteration is forward only while (cursor.hasNext()) {     console.log(cursor.next()); } // use first(), last() and all() to retrieve matched objects cursor.first(); cursor.last(); cursor.all(); // Filter non-matched objects ( var result = query.remove(collection); 标签:Mingo

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值