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.
Related
I am trying to create a json representation of mysql data in a restful webservice using php. my returned json should look something like this:
"worktypes" :
[
{
"id" : "1",
"name" : "نوع 1",
"isactive" : "true"
},
{
"id" : "2",
"name" : "نوع 2",
"isactive" : "false"
}
]
one of my options was to iterate through my result records and echo each line by hand. It was working until I reached utf8 characters shown above (like 'نوع 1') and instead of ending up with something like \u00d9\u0086\u00d9\u0088\u00d8\u00b9 I got unreadable symbols like ÃÂÃÂù çÃÂÃÂ.
If I could use json_encode somehow to create this layout, it would automatically solve this problem;But I don't know how to create such a structure in php that its json_encoded string represents my desired layout.
My other option is to somehow encode these characters myself. But I don't know how to do that either.
Could anyone help me with my problem?
UPDATE: I use a simple echo in my webservice when I get unusual characters like :
$result = array("1" => "نوع 1")
echo $result;
//results in 'ÙÙع 1' when called using webservice
The equivalent PHP array to what you have given is:
$x = array(
"worktypes" => array(
array(
"id" => "1",
"name" => "نوع 1",
"isactive" => "true"
),
array(
"id" => "2",
"name" => "نوع 2",
"isactive" => "false"
)
)
);
You can then use json_encode.
Note that you may still see escaped characters, but the browser should be able to handle unescaping them when it parses the JSON.
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
Here is my JSON Array
{
"_id" : ObjectId("563b57c84abf457b395076f0"),
"project_id" : "563b57c84abf45ce1f5076f1",
"project_task" : [
{
"switch" : "Ball",
"deviceId" : "dasdqwdf124",
"slot" : "2344",
"MigrationStartDate" : "",
"MigrationEndDate" : "",
"MigrationStatus" : "",
"task_id" :12
}
]
}
In this array project_id and task_id (Sub Document element) values are unique.
So in this case i need update my sub document element one at a time When project_id and task_id are equals to given values.
So far i used update function with and condition and findAndModify() function but never worked for me.
Here is my Query using findAndModify()
findAndModify(array('project_id' => "$projectId", 'project_task' => array('task_id' => "$taskId")), array('$set' => array("$column" => "$value")))
Please help me out
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 new to MongoDb and I've made a simple PHP filtering application (similar to TAGS)
I Have a collection which is called "Filters"
Inside of it, each record looks like this:
"_id" : ObjectId("5274da3e040b61fa15000001"),
"activation" : 1,
"and_or" : "",
"description" : "",
"id" : 13,
"order_id" : 2,
"slug" : [
"apple",
"orange",
"banana"
],
"title" : "Sample"
I take those values from a PHP form and insert them like a record.
The problem is
When I try to remove "orange" from slug, the array breaks and turns into an Object
"slug" : {
"0" : "apple",
"2" : "banana"
},
And I can no longer query it like this, as it returns NULL
db.filters.find({slug: "apple"})
null
I assume that when an array's index is numerically correct, (1, 2, 3, 4) then Mongo will recognize it as an array, otherwise it stores it as an object as custom index is used.
Unfortunately, I cannot change the indexes of the array because they are linked in other documents.
Any ideas?
Thanks!
You are getting null because you might be removing that document using remove method in php.
Instead you need to update slug to array with only apple as its element
db.collection.update($array);