doubingling4706 2014-08-10 14:25
浏览 45
已采纳

使用php更新mongo中的文档列表

I looked on stack already and can't find the same problem as mine:

I have two collections: 1) users and 2) data.

users:

{
  _id: "1234",
  name: "John"
}

and data:

{
  "userId":"1234",
  "data": {
    {...}
}

What I do now is I read all "data" documents and update the users collection for each users, to add the data (and perform more operations). It's a loop where for each data, I do a findOne() on user's Id, change the returned array.

Then, I save the new array in the users collection.

What I'd like is to do less call to Mongodb. My guess was to add every new "user" array in an array and at the end of my script, to send this array of users to Mongo. But I don't know how to tell him: "For each array in this array, update the collection."

Do you have any idea how to do that? Or maybe a better way to reach the same result?

I looked already on some question like How to update millions of documents independently? PHP + Mongo but the modification is only increases and aggregates, not like mine where I want to add a lot of data.

  • 写回答

1条回答 默认 最新

  • dongxi5505 2014-08-11 05:06
    关注

    Assuming you are using MongoDB 2.6+ and a version of the MongoDB PHP driver newer than 1.5.0, you can take advantage of the Bulk Write API via the MongoInsertBatch and MongoUpdateBatch classes in the PHP driver.

    Versions of MongoDB older than 2.6 support a more limited batch insert API (available in the PHP driver via MongoCollection::batchInsert()).

    The general strategy with bulk/batch inserts is to reduce network round trip time by sending multiple requests in a batch instead of individually.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?