i actually have a issue with mongodb im trying to update/add a value in a nested array.
{
"_id" : ObjectId("56c37e98aff662100900002a"),
"name" : "michell",
"game" : [{
"name" : "GTA",
"badges" : [{
"name" : "pacifist"
}, {
"name" : "killemall"
}]
}]
}
you can find below the way i tried but actually it just rewrite the badges array of create new game array
$collection->update(array('_id' =>new MongoId($id),'jeux.name'=>$name), array('$set' => array('jeux'=>array('name' => $name,'badges'=>array('name'=>$badge)))));
i can't find the issue here if you could help me
Well i found a solution i don't know if its the best one but it seems like it work
$collection->update(array('_id' =>new MongoId($id),'jeux.name'=>$name), array('$push' => array('jeux.$.badges'=>array('name'=>$badge))));
im using the $ in jeux.$.badges
Related
This is the structure for my MongoDB documents
{
"_id" : ObjectId("5aa02da4f6c9bf20da58017b"),
"Name" : "HP Envy",
"Model" : "m6 convertible x360",
"Cost" : 1350,
"Description" : "Sleek and slim laptop designed perfectly for business p
urposes",
"Keywords" : [
"Touchscreen",
"Rotation",
"x360",
"Slim",
"HP"
],
"Reviews" : [
{
"User" : "User 1",
"Comment" : "Great Laptop",
"Rating" : 4.5
},
{
"User" : "User 2",
"Comment" : "Great Laptop",
"Rating" : 4.5
}
]
}
I want to access the data contained inside "Reviews" using php. I can access it normal using
$coll=$db->Product;
$cursor = $coll->find();
and then echo using a foreach to display in a tabular format. But i only know how to access the ID, name etc. How can i access the Reviews inside the object arrays? i tried something like this:
$test=$coll->find(array( 'Reviews.User' => 'User 1' ));
when i try to echo it it gives me an error: Catchable fatal error: Object of class MongoCursor could not be converted to string
I'm lost please help.. I apologize if i repeated the question but i couldn't find it anywhere else
Going to try this again as I think my previous post Inserting complex PHP array directly into mongodb was poorly asked, or didn't provide a tl;dr... I'm having a really hard time finding much documentation on anything past the basics.
My array looks like:
[publication] => Array
(
[_id] => 100000009
[title] => title
[author] => author
)
I'm using PHPdriver for mongodb and I directly add the array like this:
$result = $publications->insertOne([
$publication
]);
and while this works, it give me this:
{
"_id" : ObjectId("5a910438834a9c2314006cf6"),
"0" : {
"_id" : "100000009",
"title" : "uf",
"author" : "author"
}
}
How do I make it like this instead:
{
"_id" : "100000009",
"title" : "uf",
"author" : "author"
}
Without manually breaking each line out into a "id" => $publication['id'] inside the insertOne statement?
It seems you want to insert a document, not a mongodb array.
You are creating a new array wrapping your document.
Try this:
$result = $publications->insertOne($publication);
Here I need to insert another array data where there is already an existing record in MongoDB.
Example:
Already existing record in db:
{
"_id" : 1,
"name" : "main",
"sample_details" : [
{
"detail_no" : 1,
"email" : "test#gmail.com",
"name" : "test",
},
{
"detail_no" : 2,
"email" : "test12#gmail.com",
"name" : "test12",
}
],
}
Suppose I need to insert one more array data in "sample_details". Here is my code, but the position operator "$" is not supporting. May I know the exact solution for my query:
Code:
$array = array(
'sample_details.$.detail_no' => 3,
'sample_details.$.email' => "test123#gmail.com",
'sample_details.$.name' => "test123",
);
$this->mongo_db->update(table_name,array('_id'=>1),array('$set'=>$array),array('multiple'=>true));
To insert data into array you want to use operators like $push, $pushAll or $addToSet.
You can't use positional operator because citing its documentation:
the array field must appear as part of the query document.
Also it only makes sense when you're updating an array's element and you don't know its index, not inserting new things.
I have a collection in mongodb which looks like this.
"_id" : ObjectId("554c5397ccfff21e103c9869"),
"name" : "test",
"color" : [
"552ced22ccfff2d8183c986a_Jellow",
"551fdd24ccfff2362e3c9869_test"
],
"updated_at" : ISODate("2015-05-08T06:11:35.303Z"),
"created_at" : ISODate("2015-05-08T06:11:35.303Z")
I want to update only one value in the array color But when i try to update the array it removes all the values from the color array and replaces it by the new value.
Here is the code. (I AM USING JESSENGER MONGODB PACKAGE FOR LARAVEL)
$query->where($field,'regexp','/^('.$id.')_.*/')->update([$field=>$id.'_'.$name]);
How should i do it.??
What you wanna do is, either change your schema as {key: value} pair and then follow this tutorial, that will help you to sort out your problem. OR you can get all the values from color array and replace with new value and then update your document (I would not go for it coz it is a dirty approach!).
EDIT
Hey Bud! I founded this, on jenssenger docs:
Push
Add an items to an array.
DB::collection('users')->where('name', 'John')->push('items', 'boots');
DB::collection('users')->where('name', 'John')->push('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));
If you don't want duplicate items, set the third parameter to true:
DB::collection('users')->where('name', 'John')->push('items', 'boots', true);
Pull
Remove an item from an array.
DB::collection('users')->where('name', 'John')->pull('items', 'boots');
DB::collection('users')->where('name', 'John')->pull('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));
You need to use the $set operator. Not sure how it's done in Jessenger Mongodb, but it might be something like:
$query->where($field,'regexp','/^('.$id.')_.*/')
->update(['$set' => [ $field=>$id.'_'.$name]]);
Why don't change your data like this:
"_id" : ObjectId("554c5397ccfff21e103c9869"),
"name" : "test",
"color" : [
{ "id":"552ced22ccfff2d8183c986a", "name":"Jellow"},
{ "id":"551fdd24ccfff2362e3c9869", "name":"test"}
],
"updated_at" : ISODate("2015-05-08T06:11:35.303Z"),
"created_at" : ISODate("2015-05-08T06:11:35.303Z")
then you can update element by id.
I am trying to update() a specific single array in a collection, but while it works fine with $push parameter on a single, specific array, it does not work with a $set parameter.
I don't quite understand logic behind that, because when I use such an example of $pushing the element:
$post_comment = array('$push' =>
array("comments" => array(
"_id" => new MongoId(),
"comment" => htmlspecialchars($_POST['comment']),
"author" => $user->username,
"date" => new MongoDate()
)
)
);
$entries->update(array(
"_id" => $_GET["id"]), $post_comment);
It gives me an array in a MongoDB database which looks more or less like this (with four items pushed in, respectively) :
{
"_id" : "css-clearfix-explained",
"comments" : [
{
"_id" : ObjectId("540cc940af105b19133c9869"),
"comment" : "aaa",
"author" : "maciejsitko",
"date" : ISODate("2014-09-07T21:08:16.215Z")
},
{
"_id" : ObjectId("540cc943af105b19133c986a"),
"comment" : "bbb",
"author" : "maciejsitko",
"date" : ISODate("2014-09-07T21:08:19.542Z")
},
{
"_id" : ObjectId("540cc946af105b19133c986b"),
"comment" : "ccc",
"author" : "maciejsitko",
"date" : ISODate("2014-09-07T21:08:22.968Z")
}
]
}
Which is basically what I want to have, and logically, works fine according to the documentation. But when I try the same with $set as for to edit an individual comment, in the similar fashion as shown:
$edit_comment = array('$set' =>
array("comments" => array(
"_id" => new MongoId($_POST['cmt-id']),
"comment" => htmlspecialchars($_POST['edit-comment']),
"author" => $user->username,
"date" => new MongoDate()
)
)
);
$entries->update(array(
"_id" => $_GET["id"]), $edit_comment);
It outputs four different arrays in place of the previous arrays, to illustrate that, i'll show what happened when I updated first comment "aaa" to "ddd" :
{
"_id" : "css-clearfix-explained",
"comments" : {
"_id" : ObjectId("540cc940af105b19133c9869"),
"comment" : "ddd\r\n ",
"author" : "maciejsitko",
"date" : ISODate("2014-09-07T21:12:10.833Z")
}
}
All the four array elements were pretty much erased and in their place appeared four fields as four independent array elements.
How come? Shouldn't it just work just fine like the example with $push above?
You didn't specify an index within comments. Therefore, $set replaced the array comments with the associated array supplied.
If you want to update a comment, then change your query in the first argument to match a comment by a unique field. Ex, date. In the second argument use a positional $ operator.
Example:
$edit_comment = array('$set' =>
array("comments.$" => array(
"_id" => new MongoId($_POST['cmt-id']),
"comment" => htmlspecialchars($_POST['edit-comment']),
"author" => $user->username,
"date" => new MongoDate()
)
)
);
// this assumes the post date is unique. On second though use something else.
$query = array( "_id" => $_GET["id"], "comments.date" => $_POST['post-date'])
$entries->update( $query, $edit_comment);
Check this out for more info and better explanation:
MongoDB - $set to update or push Array element