Here's what the document looks like now.
Array
(
[email] => admin@mysite.com
[emailQueries] => Array
(
[0] => Array
(
[21] => 0
)
[1] => Array
(
[21] => 0
)
)
[last_visit] => 1375050871
)
Here's the code I am using to populate the emailQueries
$arrayValueToAdd = array( (string) $email_id => '0' );
$collection->update( array('email' => $user['email']),
array( '$push' =>
array( 'emailQueries' => $arrayValueToAdd )
)
);
However, I'd like to simply have the emailQueries
array look like:
[emailQueries] => Array
(
[21] => 0
[22] => 0
)
Probably a simple thing I'm overlooking...
You are using the array operator push with a data structure that is not considered by MongoDb as an array ( array must have 0-based ascending numeric indexes ). You must treat it as an object. So you have 2 choices.
If in [21] => 0
, the value 0
is useless (and I don't expect so), you can use a true array:
"emailQueries" : [21,22]
and you can use the push
operator in the PHP code like this
$collection->update( array('email' => $user['email']),
array( '$push' =>
array( 'emailQueries' => (string) $email_id )
));
On the other hand, if you must keep the value 0
, and for instance, increment it, you have to considered your EmailQuery as an object :
"emailQueries" : {
"21" : 0,
"22" : 0
}
So you can use a simple update to add field in emailQueries
:
$collection->update( array('email' => $user['email']),
array( '$set' =>
array( 'emailQueries.'.$email_id => 0 )
));
And the $inc
, for incrementation :
$collection->update( array('email' => $user['email']),
array( '$inc' =>
array( 'emailQueries.'.$email_id => 1 )
));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.