Mongodb-insert-document

提供:Dev Guides
移動先:案内検索

MongoDB-文書の挿入

この章では、MongoDBコレクションにドキュメントを挿入する方法を学びます。

insert()メソッド

MongoDBコレクションにデータを挿入するには、MongoDBの* insert()または save()*メソッドを使用する必要があります。

構文

  • 挿入()*コマンドの基本的な構文は次のとおりです-
>db.COLLECTION_NAME.insert(document)

> db.users.insert({
... _id : ObjectId("507f191e810c19729de860ea"),
... title: "MongoDB Overview",
... description: "MongoDB is no sql database",
... by: "tutorials point",
... url: "http://www.finddevguides.com",
... tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted" : 1 })
>

ここで、 mycol は、前の章で作成したコレクション名です。 データベースにコレクションが存在しない場合、MongoDBはこのコレクションを作成し、ドキュメントを挿入します。

挿入されたドキュメントで、_idパラメーターを指定しない場合、MongoDBはこのドキュメントに一意のObjectIdを割り当てます。

_idは、コレクション内のすべてのドキュメントに固有の12バイトの16進数です。 12バイトは次のように分割されます-

_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)

次に示すように、ドキュメントの配列をinsert()メソッドに渡すこともできます。

> db.createCollection("post")
> db.post.insert([
    {
        title: "MongoDB Overview",
        description: "MongoDB is no SQL database",
        by: "tutorials point",
        url: "http://www.finddevguides.com",
        tags: ["mongodb", "database", "NoSQL"],
        likes: 100
    },
    {
    title: "NoSQL Database",
    description: "NoSQL database doesn't have tables",
    by: "tutorials point",
    url: "http://www.finddevguides.com",
    tags: ["mongodb", "database", "NoSQL"],
    likes: 20,
    comments: [
        {
            user:"user1",
            message: "My first comment",
            dateCreated: new Date(2013,11,10,2,35),
            like: 0
        }
    ]
}
])
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 2,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})
>

ドキュメントを挿入するには、* db.post.save(document)も使用できます。 ドキュメントで *_id を指定しない場合、* save()メソッドは insert()*メソッドと同じように機能します。 _idを指定すると、save()メソッドで指定された_idを含むドキュメントのデータ全体が置き換えられます。

insertOne()メソッド

コレクションにドキュメントを1つだけ挿入する必要がある場合は、このメソッドを使用できます。

構文

insert()コマンドの基本的な構文は次のとおりです-

>db.COLLECTION_NAME.insertOne(document)

次の例では、empDetailsという名前の新しいコレクションを作成し、insertOne()メソッドを使用してドキュメントを挿入します。

> db.createCollection("empDetails")
{ "ok" : 1 }
> db.empDetails.insertOne(
    {
        First_Name: "Radhika",
        Last_Name: "Sharma",
        Date_Of_Birth: "1995-09-26",
        e_mail: "[email protected]",
        phone: "9848022338"
    })
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5dd62b4070fb13eec3963bea")
}
>

insertMany()メソッド

insertMany()メソッドを使用して複数のドキュメントを挿入できます。 このメソッドには、ドキュメントの配列を渡す必要があります。

次の例では、insertMany()メソッドを使用して、3つの異なるドキュメントをempDetailsコレクションに挿入します。

> db.empDetails.insertMany(
    [
        {
            First_Name: "Radhika",
            Last_Name: "Sharma",
            Date_Of_Birth: "1995-09-26",
            e_mail: "[email protected]",
            phone: "9000012345"
        },
        {
            First_Name: "Rachel",
            Last_Name: "Christopher",
            Date_Of_Birth: "1990-02-16",
            e_mail: "[email protected]",
            phone: "9000054321"
        },
        {
            First_Name: "Fathima",
            Last_Name: "Sheik",
            Date_Of_Birth: "1990-02-16",
            e_mail: "[email protected]",
            phone: "9000054321"
        }
    ]
)
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5dd631f270fb13eec3963bed"),
        ObjectId("5dd631f270fb13eec3963bee"),
        ObjectId("5dd631f270fb13eec3963bef")
    ]
}
>