English 中文(简体)
mongo在文件上加插田(栏)
原标题:mongo add field to document ($set doesn t)
  • 时间:2011-11-23 04:25:51
  •  标签:
  • php
  • mongodb

我想:

  • fetch some documents
  • for every document, set a field:
    • change it, if it exists
    • add it, if it doesn t

我做的是:

// fresh data
print_r(iterator_to_array($collection->find()->limit(2)));

// query
$docs = $collection->find()->limit(2);
// fetch
foreach ( $docs AS $id => $doc ) {
  // update
  $collection->update(array( _id  => $doc[ _id ]), array(
     $set  => array(
       existing_field  =>  x ,
       new_field  =>  y ,
    ),
  ), array( multiple  => false));
}

// verify
print_r(iterator_to_array($collection->find()->limit(2)));

为什么不做什么? 现有领域没有变化,新领域也增加了。 条件可能是错误的吗?

PS. The full code (with a lot of junk in between): http://pastebin.com/CNfCVxex
lines 33 - 37 -- fresh data
lines 63 - 76 -- query, fetch & update
lines 79 - 80 -- verify

最佳回答

你不熟悉你在此使用的司机,但从您的描述来看,你无法在被打的单一数据库中实现你想要的东西,无需打耳/lo。

定购人将根据是否存在外地情况,插入或更新外地。

db.Collection.update({}, { $set : { "myfield" : "x" } }, false, true)

以上所述将把我的实地定位在收集到x的所有文件中,或者如果已经存在,则将价值改变为x。 你们想要达到的目标吗?

问题回答

我怀疑你需要将“座标”改为“Mongo”。 Id by using new MongoId. 见网页底部:





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签