zoukankan      html  css  js  c++  java
  • 6.Insert Documents-官方文档摘录

    总结

    1.插入单文档

    db.inventory.insertOne(
       { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
    )

    db.inventory.find({item:"canvas"})

    2.插入多文档

    db.inventory.insertMany([
       { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
       { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
       { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
    ])
    # 返回多行
    db.inventory.find({})

    This page provides examples of insert operations in MongoDB.

    CREATING A COLLECTION

    If the collection does not currently exist, insert operations will create the collection.

    Insert a Single Document

    New in version 3.2.

    db.collection.insertOne() inserts a single document into a collection.

    The following example inserts a new document into the inventory collection. If the document does not specify an _id field, MongoDB adds the _id field with an ObjectId value to the new document. See Insert Behavior.

    db.inventory.insertOne(
       { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
    )
    

    You can run the operation in the web shell below:

    insertOne() returns a document that includes the newly inserted document’s _id field value. For an example of a return document, see db.collection.insertOne() reference.

    To retrieve the document that you just inserted, query the collection:

    db.inventory.find( { item: "canvas" } )
    

    Insert Multiple Documents

    New in version 3.2.

    db.collection.insertMany() can insert multiple documents into a collection. Pass an array of documents to the method.

    The following example inserts three new documents into the inventory collection. If the documents do not specify an _id field, MongoDB adds the _id field with an ObjectId value to each document. See Insert Behavior.

    db.inventory.insertMany([
       { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
       { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
       { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
    ])
    

    You can run the operation in the web shell below:

    insertMany() returns a document that includes the newly inserted documents _id field values. See thereference for an example.

    To retrieve the inserted documents, query the collection:

    db.inventory.find( {} )
    

    Insert Behavior

    Collection Creation

    If the collection does not currently exist, insert operations will create the collection.

    _id Field

    In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _idfield.

    This also applies to documents inserted through update operations with upsert: true.

    Atomicity

    All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions

    Write Acknowledgement

    With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

  • 相关阅读:
    Linux下安装软件遇见的问题汇总
    使用AlarmManager定期执行工作
    android打开文件、保存对话框、创建新文件夹对话框(转载)
    一些算法的整理
    安卓adb调试命令常见的问题
    java下的串口通信-RXTX
    Unity 协程运行时的监控和优化
    Unity3D 协程的介绍和使用
    游戏服务器:到底使用UDP还是TCP
    Unity 可重复随机数
  • 原文地址:https://www.cnblogs.com/olinux/p/7232581.html
Copyright © 2011-2022 走看看