如何插入json数据以及Mongo DB?

时间:2017-07-28 13:07:24

标签: json mongodb

这是我的Json数据

 0:
{name: "Dubai Campaign", status: "ACTIVE", start_time: "2017-06-
30T12:07:49+0530", id: "23842596088330291", objective: 
"LEAD_GENERATION"}
1:
{
 name: "Onam Campaign", status: "PAUSED", start_time: "2017-06-
 27T19:16:56+0530", id: "23842595270760291", objective: 
 "LEAD_GENERATION"
 }

如何插入json数据以及Mongo DB?如

  

db.userdata.insert({});

2 个答案:

答案 0 :(得分:1)

要在单个查询中插入多个文档,您可以像我一样在insert命令中传递文档数组,如下所示

db.userdata.insert([{name: "Dubai Campaign", status: "ACTIVE", start_time: "2017-06-30T12:07:49+0530", id: "23842596088330291", objective: "LEAD_GENERATION"},{name: "Onam Campaign", status: "PAUSED", start_time: "2017-06-27T19:16:56+0530", id: "23842595270760291", objective: "LEAD_GENERATION"}])

并且要插入文档,您还可以使用db.userdata.save(yourDocument)

答案 1 :(得分:0)

使用insert

db.userdata.insertOne(
    {name: "Dubai Campaign", status: "ACTIVE", start_time: "2017-06-30T12:07:49+0530", id: "23842596088330291", objective: "LEAD_GENERATION"}
)

您还可以在一次通话中插入多个文档......

db.userdata.insertMany([
    {name: "Dubai Campaign", status: "ACTIVE", start_time: "2017-06-30T12:07:49+0530", id: "23842596088330291", objective: "LEAD_GENERATION"},
    {name: "Onam Campaign", status: "PAUSED", start_time: "2017-06-27T19:16:56+0530", id: "23842595270760291", objective: "LEAD_GENERATION"}
])