博客
关于我
MongoDB 文档的查询和插入操作
阅读量:753 次
发布时间:2019-03-23

本文共 1709 字,大约阅读时间需要 5 分钟。

MongoDB是文档型数据库,与传统的关系型数据库有着显著的不同,理解这些差异对于有效地使用MongoDB至关重要。文档结构类似于数据库中的row,每个文档由key/value对组成,key和value用冒号(:)分隔,多个key/value对用逗号(,)分隔。例如:

user = {  name: "sue",  age: 24}

MongoDB支持将文档存储在文档数组中,这对于批量插入和批量更新操作特别有用。例如:

userArray = [  { name: "sue", age: 24 },  { name: "joe", age: 25 },  { name: "pei", age: 32 }];

在开始操作前,可以使用use test命令切换到测试数据库,以避免对生产环境造成影响。

插入操作

MongoDB提供了几种插入方法:

  • 单个文档插入:使用db.collection.insert()db.collection.insertOne()或直接传递文档:

    user = { name: "test1", age: 22 };db.users.insert(user);db.users.insert({ name: "test1", age: 22 });db.users.insertOne({ name: "test1", age: 22 });
  • 批量插入文档:使用db.collection.insertMany()来插入文档数组:

    db.users.insert([user1, user2, user3]);db.users.insertMany([user1, user2, user3]);
  • 查找操作

    查询操作是MongoDB中最常用的操作之一,基础的查询语法如下:

    db.collection.find(  
    ,
    );

    Query Filter是用来过滤文档的条件,而Projection用于指定要返回的字段集合,默认是返回所有字段。如果不指定_id字段的投影,默认会包含_id。

    常见的Query运算符

  • 等式匹配

    db.users.find({ age: 21 });db.users.find({ age: { $eq: 21 } });
  • 不等式匹配

    db.users.find({ age: { $ne: 21 } });
  • 范围匹配

    db.users.find({ age: { $lt: 22 } });db.users.find({ age: { $gt: 22 } });
  • 逻辑运算符

    $or、$and:db.users.find({ $or: [{ age: 21 }, { age: 22 }] });
  • 集合匹配

    db.users.find({ age: { $in: [21, 22] } });
  • 集合不匹配

    db.users.find({ age: { $nin: [21, 22] } });
  • Projection(投影)

    为了提高查询效率,可以通过Projection指定返回的字段:

    db.users.find({ age: 21 }, { name: 1, _id: 0 });

    这将返回匹配的所有文档,只包含指定的字段。去除_id字段可以通过设定projection选项来实现。

    Cursor And Iteration

    db.collection.find()返回一个Cursor,用户可以通过以下方法迭代Cursor:

  • 使用var声明Cursor:

    var us = db.users.find();
  • 手动迭代:

    while (us.hasNext()) {  print(tojson(us.next()));}
  • 使用forEach

    db.users.find().forEach(function (doc) {  print(tojson(doc));});
  • 通过明确声明Cursor,可以避免MongoDB自动迭代,默认会显示前20个文档,用户应谨慎利用.find()方法。

    转载地址:http://zsfzk.baihongyu.com/

    你可能感兴趣的文章
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>
    npm切换到淘宝源
    查看>>
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm升级以及使用淘宝npm镜像
    查看>>
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>