how to upload same city into mongo db
-                 db.collection.insertMany()
-                 mongosh Method This is a mongoshmethod. This is non the documentation forNode.jsor other programming language specific commuter methods.In most cases, mongoshmethods work the same way every bit the legacymongocrush methods. However, some legacy methods are unavailable inmongosh.For the legacy mongoshell documentation, refer to the documentation for the respective MongoDB Server release:- mongo shell v4.4
- mongo beat v4.2
- mongo shell v4.0
 For MongoDB API drivers, refer to the language specific MongoDB driver documentation. Inserts multiple documents into a drove. The insertMany()method has the following syntax:db .collection .insertMany ( [ <document 1> , <document 2>, ... ], { writeConcern: <document>, ordered: <boolean> } ) Parameter Type Description certificatedocument An array of documents to insert into the collection. writeConcerncertificate Optional. A document expressing the write business organisation. Omit to apply the default write concern. Do not explicitly prepare the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Business organization. orderedboolean Optional. A boolean specifying whether the mongodinstance should perform an ordered or unordered insert. Defaults totrue.Returns : A document containing: - An                            acknowledgedboolean, set totrueif the operation ran with write concern orimitationif write concern was disabled
- An                            insertedIdsassortment, containing_idvalues for each successfully inserted certificate
 
Given an array of documents,              insertMany()              inserts each certificate in the array into the drove.
By default documents are inserted in order.
If                ordered                is fix to simulated, documents are inserted in an unordered format and may be reordered by                mongod                to increase operation. Applications should not depend on ordering of inserts if using an unordered                insertMany().
The number of operations in each grouping cannot exceed the value of the maxWriteBatchSize of the database. As of MongoDB 3.6, this value is                100,000. This value is shown in the                hello.maxWriteBatchSize                field.
This limit prevents issues with oversized error messages. If a group exceeds this limit, the customer driver divides the group into smaller groups with counts less than or equal to the value of the limit. For example, with the                maxWriteBatchSize                value of                100,000, if the queue consists of                200,000                operations, the driver creates 2 groups, each with                100,000                operations.
The driver simply divides the group into smaller groups when using the loftier-level API. If using db.runCommand() straight (for case, when writing a driver), MongoDB throws an error when attempting to execute a write batch which exceeds the limit.
Starting in MongoDB iii.6, one time the fault report for a single batch grows besides big, MongoDB truncates all remaining error messages to the empty string. Currently, begins once there are at to the lowest degree 2 error letters with total size greater than                1MB.
The sizes and group mechanics are internal functioning details and are subject to modify in future versions.
Executing an                ordered                list of operations on a sharded collection will generally exist slower than executing an                unordered                list since with an ordered list, each performance must wait for the previous operation to finish.
If the collection does not exist, then                insertMany()                creates the drove on successful write.
If the certificate does not specify an _id field, then                mongod                adds the                _id                field and assign a unique                ObjectId()                for the certificate. Most drivers create an ObjectId and insert the                _id                field, but the                mongod                will create and populate the                _id                if the commuter or awarding does non.
If the document contains an                _id                field, the                _id                value must be unique within the collection to avoid duplicate key mistake.
                insertMany()                is non compatible with                db.drove.explain().
Inserts throw a                BulkWriteError                exception.
Excluding Write Business organisation errors, ordered operations finish afterward an fault, while unordered operations continue to procedure whatsoever remaining write operations in the queue.
Write concern errors are displayed in the                writeConcernErrors                field, while all other errors are displayed in the                writeErrors                field. If an error is encountered, the number of successful write operations are displayed instead of a list of inserted _ids. Ordered operations brandish the single mistake encountered while unordered operations display each mistake in an assortment.
                db.collection.insertMany()                tin can be used within multi-certificate transactions.
In most cases, multi-document transaction incurs a greater performance toll over single document writes, and the availability of multi-certificate transactions should non be a replacement for effective schema design. For many scenarios, the denormalized data model (embedded documents and arrays) will keep to exist optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.
For additional transactions usage considerations (such equally runtime limit and oplog size limit), see also Production Considerations.
Starting in MongoDB four.four, you lot can create collections and indexes inside a multi-certificate transaction if the transaction is non a cross-shard write transaction.
Specifically, in MongoDB 4.iv and greater, if you specify an insert on a non-existing drove in a transaction, the collection is implicitly created.
In MongoDB four.four and earlier, the operation must be run on an existing collection.
Practice non explicitly ready the write business concern for the functioning if run in a transaction. To utilise write concern with transactions, see Transactions and Write Concern.
The following examples insert documents into the              products              collection.
The post-obit example uses                db.collection.insertMany()                to insert documents that do non incorporate the                _id                field:
                                                                                                                                          try                                                        {                                                                                                                                    db                            .products                            .insertMany                            (                             [                                                                                                                                    {                            item:                            "card",                            qty:                            15                            }                            ,                                                                                                                                    {                            item:                            "envelope",                            qty:                            20                            }                            ,                                                                                                                                    {                            particular:                            "stamps"                                                        ,                            qty:                            thirty                            }                                                                                                        ] )                            ;                                                                            }                            take hold of                                                        (                            east)                             {                                                                                                                                    print                             (                            e)                            ;                                                                            }                                                                    
                                                The operation returns the following document:
                                                                                                              {                                                                                                        "acknowledged"                                                        :                            true,                                                                                                        "insertedIds"                                                        :                             [                                                                                                                                    ObjectId                            ("562a94d381cb9f1cd6eb0e1a")                            ,                                                                                                                                    ObjectId                            ("562a94d381cb9f1cd6eb0e1b")                            ,                                                                                                                                    ObjectId                            ("562a94d381cb9f1cd6eb0e1c")                                                                                                        ]                                                                            }                                                                    
                                                Because the documents did not include                _id,                mongod                creates and adds the                _id                field for each document and assigns it a unique                ObjectId()                value.
The                ObjectId                values are specific to the auto and time when the operation is run. Equally such, your values may differ from those in the example.
The following example/performance uses                insertMany()                to insert documents that include the                _id                field. The value of                _id                must be unique within the drove to avoid a duplicate cardinal error.
                                                                                                                                          try                                                        {                                                                                                                                    db                            .products                            .insertMany                            (                             [                                                                                                                                    {                            _id:                            10,                            item:                            "big box",                            qty:                            twenty                            }                            ,                                                                                                                                    {                            _id:                            11,                            item:                            "small-scale box",                            qty:                            55                            }                            ,                                                                                                                                    {                            _id:                            12,                            item:                            "medium box",                            qty:                            30                            }                                                                                                        ] )                            ;                                                                            }                            catch                                                        (                            e)                             {                                                                                                                                    impress                             (                            e)                            ;                                                                            }                                                                    
                                                The operation returns the post-obit certificate:
                                      {                      "acknowledged"                                            :                      truthful,                      "insertedIds"                                            :                       [                      10,                      11,                      12                      ] }                                                Inserting a duplicate value for any fundamental that is part of a unique index, such every bit                _id, throws an exception. The post-obit attempts to insert a document with a                _id                value that already exists:
                                                                                                                                          try                                                        {                                                                                                                                    db                            .products                            .insertMany                            (                             [                                                                                                                                    {                            _id:                            13,                            particular:                            "envelopes",                            qty:                            60                            }                            ,                                                                                                                                    {                            _id:                            13,                            detail:                            "stamps",                            qty:                            110                            }                            ,                                                                                                                                    {                            _id:                            14,                            item:                            "packing tape",                            qty:                            38                            }                                                                                                        ] )                            ;                                                                            }                            grab                                                        (                            due east)                             {                                                                                                                                    impress                             (                            e)                            ;                                                                            }                                                                    
                                                Since                _id: xiii                already exists, the following exception is thrown:
                                                                                                              BulkWriteError                            (                            {                                                                                                        "writeErrors"                                                        :                             [                                                                                                                                    {                                                                                                        "alphabetize"                                                        :                            0,                                                                                                        "code"                                                        :                            11000,                                                                                                        "errmsg"                                                        :                            "E11000 indistinguishable central error collection: inventory.products alphabetize: _id_ dup key: { : 13.0 }",                                                                                                        "op"                                                        :                             {                                                                                                        "_id"                                                        :                            xiii,                                                                                                        "item"                                                        :                            "stamps",                                                                                                        "qty"                                                        :                            110                                                                                                                                  }                                                                                                        }                                                                                                        ]                            ,                                                                                                        "writeConcernErrors"                                                        :                             [                             ]                            ,                                                                                                        "nInserted"                                                        :                            one,                                                                                                        "nUpserted"                                                        :                            0,                                                                                                        "nMatched"                                                        :                            0,                                                                                                        "nModified"                                                        :                            0,                                                                                                        "nRemoved"                                                        :                            0,                                                                                                        "upserted"                                                        :                             [                             ]                                                                            })                                                                    
                                                Annotation that one document was inserted: The commencement certificate of                _id: 13                will insert successfully, but the second insert will neglect. This will also finish additional documents left in the queue from being inserted.
With                ordered                to                false, the insert functioning would continue with whatsoever remaining documents.
The following attempts to insert multiple documents with                _id                field and                ordered: false. The assortment of documents contains ii documents with duplicate                _id                fields.
                                                                                                                                          try                                                        {                                                                                                                                    db                            .products                            .insertMany                            (                             [                                                                                                                                    {                            _id:                            10,                            detail:                            "large box",                            qty:                            twenty                            }                            ,                                                                                                                                    {                            _id:                            xi,                            particular:                            "minor box",                            qty:                            55                            }                            ,                                                                                                                                    {                            _id:                            11,                            item:                            "medium box",                            qty:                            30                            }                            ,                                                                                                                                    {                            _id:                            12,                            particular:                            "envelope",                            qty:                            100}                            ,                                                                                                                                    {                            _id:                            13,                            item:                            "stamps",                            qty:                            125                            }                            ,                                                                                                                                    {                            _id:                            13,                            item:                            "tape",                            qty:                            20}                            ,                                                                                                                                    {                            _id:                            14,                            detail:                            "chimera wrap",                            qty:                            thirty}                                                                                                        ]                            ,                             {                            ordered:                            imitation                            } )                            ;                                                                            }                            catch                                                        (                            e)                             {                                                                                                                                    print                             (                            e)                            ;                                                                            }                                                                    
                                                The operation throws the post-obit exception:
                                                                                                              BulkWriteError                            (                            {                                                                                                        "writeErrors"                                                        :                             [                                                                                                                                    {                                                                                                        "index"                                                        :                            2,                                                                                                        "lawmaking"                                                        :                            11000,                                                                                                        "errmsg"                                                        :                            "E11000 duplicate central fault collection: inventory.products index: _id_ dup key: { : 11.0 }",                                                                                                        "op"                                                        :                             {                                                                                                        "_id"                                                        :                            11,                                                                                                        "item"                                                        :                            "medium box",                                                                                                        "qty"                                                        :                            30                                                                                                                                  }                                                                                                        }                            ,                                                                                                                                    {                                                                                                        "alphabetize"                                                        :                            5,                                                                                                        "code"                                                        :                            11000,                                                                                                        "errmsg"                                                        :                            "E11000 duplicate key mistake drove: inventory.products index: _id_ dup key: { : 13.0 }",                                                                                                        "op"                                                        :                             {                                                                                                        "_id"                                                        :                            13,                                                                                                        "particular"                                                        :                            "record",                                                                                                        "qty"                                                        :                            xx                                                                                                                                  }                                                                                                        }                                                                                                        ]                            ,                                                                                                        "writeConcernErrors"                                                        :                             [                             ]                            ,                                                                                                        "nInserted"                                                        :                            5,                                                                                                        "nUpserted"                                                        :                            0,                                                                                                        "nMatched"                                                        :                            0,                                                                                                        "nModified"                                                        :                            0,                                                                                                        "nRemoved"                                                        :                            0,                                                                                                        "upserted"                                                        :                             [                             ]                                                                            })                                                                    
                                                While the document with                item: "medium box"                and                item: "tape"                failed to insert due to duplicate                _id                values,                nInserted                shows that the remaining v documents were inserted.
Given a three member replica fix, the post-obit operation specifies a                w                of                majority                and                wtimeout                of                100:
                                                                                                                                          try                                                        {                                                                                                                                    db                            .products                            .insertMany                            (                                                                                                                                    [                                                                                                                                    {                            _id:                            x,                            particular:                            "big box",                            qty:                            twenty                            }                            ,                                                                                                                                    {                            _id:                            11,                            detail:                            "small box",                            qty:                            55                            }                            ,                                                                                                                                    {                            _id:                            12,                            particular:                            "medium box",                            qty:                            30                            }                                                                                                        ]                            ,                                                                                                                                    {                            westward:                            "majority",                            wtimeout:                            100                            }                                                                                                        )                            ;                                                                            }                            catch                                                        (                            e)                             {                                                                                                                                    print                             (                            e)                            ;                                                                            }                                                                    
                                                If the primary and at to the lowest degree 1 secondary acknowledge each write operation inside 100 milliseconds, it returns:
                                                                                                              {                                                                                                        "acknowledged"                                                        :                            true,                                                                                                        "insertedIds"                                                        :                             [                                                                                                                                    ObjectId                            ("562a94d381cb9f1cd6eb0e1a")                            ,                                                                                                                                    ObjectId                            ("562a94d381cb9f1cd6eb0e1b")                            ,                                                                                                                                    ObjectId                            ("562a94d381cb9f1cd6eb0e1c")                                                                                                        ]                                                                            }                                                                    
                                                If the total time required for all required nodes in the replica prepare to admit the write performance is greater than                wtimeout, the post-obit                writeConcernError                is displayed when the                wtimeout                menstruation has passed.
This performance returns:
                                                                                                              WriteConcernError                            (                            {                                                                                                        "code"                                                        :                            64,                                                                                                        "errmsg"                                                        :                            "waiting for replication timed out",                                                                                                        "errInfo"                                                        :                             {                                                                                                        "wtimeout"                                                        :                            true,                                                                                                        "writeConcern"                                                        :                             {                            // Added in MongoDB four.4                                                                                                                                  "w"                                                        :                            "majority",                                                                                                        "wtimeout"                                                        :                            100,                                                                                                        "provenance"                                                        :                            "getLastErrorDefaults"                                                                                                                                  }                                                                                                        }                                                                            })                                                                    
                                                Source: https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/
Post a Comment for "how to upload same city into mongo db"