如何将信息附加到mongo中的文档? [英] how to append information to a document in mongo?

查看:24
本文介绍了如何将信息附加到mongo中的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景信息

我的 mongo 数据库中有以下数据:

I have the following data in my mongo database:

{ "_id" : 
       ObjectId("581c97b573df465d63af53ae"), 
       "ph" : "+17771111234", 
       "fax" : false, 
       "city" : "abd", 
       "department" : "", 
       "description" : "a test" 
}

我现在正在编写一个脚本,该脚本将遍历包含我需要附加到文档的数据的 CSV 文件.例如,数据可能如下所示:

I am now writing a script that will loop through a CSV file that contains data that I need to append to the document. For example, the data might look like this:

+17771111234, 10:15, 12:15, test@yahoo.com
+17771111234, 1:00, 9:00, anothertest@yahoo.com

最终我想得到一个看起来像这样的 mongo 文档:

Ultimately I want to end up with a mongo document that looks like this:

{ "_id" : 
       ObjectId("581c97b573df465d63af53ae"), 
       "ph" : "+17771111234", 
       "fax" : false, 
       "city" : "abd", 
       "department" : "", 
       "description" : "a test",
       "contact_locations": [
           {
              "stime": "10:15", 
              "etime": "12:15", 
              "email": "test@yahoo.com"
           },
           {
              "stime": "1:00", 
              "etime": "9:00", 
              "email": "anothertest@yahoo.com"
           },
       ]
}

问题

我编写的代码实际上是创建新文档而不是附加到现有文档.实际上,它甚至没有在 CSV 文件中的每行创建一个新文档......我还没有进行足够的调试,无法真正理解原因.

The code I've written is actually creating new documents instead of appending to the existing ones. And actually, it's not even creating a new document per row in the CSV file... which I haven't debugged enough yet to really understand why.

代码

对于 csv 文件中的每一行,我正在运行以下逻辑

For each row in the csv file, I'm running the following logic

while(!$csv->eof() && ($row = $csv->fgetcsv()) && $row[0] !== null) { 
   //code that massages the $row into the way I need it to look.
   $data_to_submit = array('contact_locations' => $row);
   echo "proving that the record already exists...: <BR>";
   $cursor = $contact_collection->find(array('phnum'=>$row[0]));   
   var_dump(iterator_to_array($cursor));

   echo "now attempting to update it....<BR>";
   // $cursor = $contact_collection->update(array('phnum'=>$row[0]), $data_to_submit, array('upsert'=>true));
        $cursor = $contact_collection->insert(array('phnum'=>$row[0]), $data_to_submit);
   echo "AFTER UPDATE <BR><BR>";
   $cursor = $contact_collection->find(array('phnum'=>$row[0]));
   var_dump(iterator_to_array($cursor));
   }
}

问题

  1. 有没有办法附加"到文档?或者我是否需要获取现有文档,另存为数组,将我的联系人位置数组与主文档合并,然后重新保存?

  1. Is there a way to "append" to documents? Or do I need to grab the existing document, save as an array, merge my contact locations array with the main document and then resave?

如何查询contact_locations"对象是否已存在于文档中?

how can I query to see if the "contact_locations" object already exists inside a document?

推荐答案

是的,你可以做到!

首先,您需要找到您的文档并推送您需要的新值:

1st you need to find your document and push the new value you need :

使用 findAndModify$addToSet :

$cursor = $contact_collection->findAndModify(
     array("ph" => "+17771111234"),
     array('$addToSet' => 
        array(
            "contact_locations" => array(
                 "stime"=> "10:15", 
                 "etime"=> "12:15", 
                 "email"=> "test@yahoo.com"
            )
        )
     )
);

最好的部分是 $addToSet 不会添加 2 次相同的东西,所以你不会有两次相同的值 :)

The best part is $addToSet wont add 2 time the same stuff so you will not have twice the same value :)

这里的文档 https://docs.mongodb.com/manual/reference/operator/update/addToSet/

这篇关于如何将信息附加到mongo中的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆