Hi all i am trying to push values to existing record using $push but i am getting error stating that
Invalid modifier specified: $push
I am using php below is my code php code
$collection->update(array('_id'=>3,"data._id"=>2),array('$push'=>array('userid','52')));
i.e adding 52 to userid. In 3 record and data._id 2
below is my table structure for mongo db
{ "_id" : 2,
"name" : "test",
"data" :[{"_id" : "1",
"file" : "nic",
"userid" : [1,2 ]
},
{"_id" : "2",
"file" : "nic1",
"userid" : [1 ]
},
{"_id" : 3,
"file" : "nick2",
"userid" : [1,2 ]
}
]},
{ "_id" : 3,
"name" : "test",
"data" : [{"_id" : "1",
"file" : "nic",
"userid" : [1,2 ]
},
{"_id" : "2",
"file" : "nic1",
"userid" : [3,2 ]
}
]}
Use the $ positional operator in your update that identifies an element in the data array to update without explicitly specifying the position of the element:
$collection -> update(
array('_id' => 3, "data._id" => 2),
array('$push' =>
array('data.$.userid' => 52)
)
);
Related
How to update members age whose name is TEST1 using yii2.?
Used below code to update , but i am specifying the indexes there , i want with out specifying the indexes.
User::updateAll([ '$set'=> ['Addresses.0.members.0.age'=>100] ],['IN','Addresses.members.name',['TEST1'] ]);
{
"_id" : ObjectId("595209b65312f48195fb2e01"),
"username" : "Test name",
"Addresses" : [
{
"address_no" : 1,
"Address" : "Test house",
"City" : "test city",
"State" : "Test state",
"Mobile" : "9999999",
"members" : [
{
"name" : "TEST1",
"age" : 35
},
{
"name" : "TEST2",
"age" : 30
},
]
},
{
"address_no" : 2,
"Address" : "2B, Test place",
"City" : "Test city",
"State" : "Test State",
"Pincode" : "12345",
"Phone" : "1234568789",
"Mobile" : 9999999999
}
],
"Beneficiaries" : [
{
"beneficiary_id" : 1,
"Name" : "Test1",
"Age" : "28",
"Sex" : "F"
}
],
"auth_key" : "esd8d89ds89ds89ds89ds",
}
there is position operator $ to do this kind of job
{
"Addresses.members.name" : "TEST2",
},
{
$set: {
"Addresses.$.members.0.age" : 40
}
}
Here I specified first index as it supports up to one level depth.
New feature might release in future to resolve this issue: https://jira.mongodb.org/browse/SERVER-831
Yii::$app->mongodb->getCollection('user')->update(['_id' => $id, 'members.name' => 'Test1'], ['$set' => [
'members.$.age' => 100,
]]);
I am using a MVC model in PHP. I am getting the following string from the View layer (This is a angular.js array but I am getting it as a string):
[
{
"name" : "item",
"price" : "123",
"quantity" : 12,
"id" : 1
}, {
"name" : "hhh",
"price" : "000",
"quantity" : 12,
"id" : 2
}, {
"name" : "kk",
"price" : "88",
"quantity" : 12,
"id" : 3
}
]
How can I extract the values of name, price, quantity and id from this string and put that into insert query?
This is what is known as a serialized array, meaning that it is a JavaScript array in string form (JSON). You can use PHP's json_decode function to deserialize the string, from there you can use it as a normal array:
$json='[
{
"name" : "item",
"price" : "123",
"quantity" : 12,
"id" : 1
}, {
"name" : "hhh",
"price" : "000",
"quantity" : 12,
"id" : 2
}, {
"name" : "kk",
"price" : "88",
"quantity" : 12,
"id" : 3
}
]';
$array=json_decode($json);
foreach ($array as &$value) {
var_export($value->name);
var_export($value->price);
var_export($value->quantity);
var_export($value->id);
}
The above should display all the values in your array. I'm not sure what you mean by "put that into the insert query", but hopefully the above will help you get access to this data.
So my collection looks like this:
{
"_id" : ObjectId("52722429d874590c15000029"),
"name" : "Bags",
"products" : [{
"_id" : ObjectId("527225b5d87459b802000029"),
"name" : "Prada",
"description" : "Prada Bag",
"points" : "234",
"validDate" : 1382562000,
"link" : "dasdad",
"code" : "423423424",
"image" : null
}, {
"_id" : ObjectId("5272307ad87459401a00002a"),
"name" : "Gucci",
"description" : "Gucii bag",
"points" : "2342",
"validDate" : 1383170400,
"link" : "dsadada",
"code" : "2342",
"image" : null
}]
}
and I want to get only the product with the _id 527225b5d87459b802000029, I tried this:
$this->find(array(
'_id' => new \MongoId('52722429d874590c15000029'),
'products._id' => new \MongoId('527225b5d87459b802000029')
));
But it returns the entire array for that collection, and I only want one...can this be done in mongo?
As mentioned in comments, you have to add a projection, and more precisely an $elemMatch. No need to use the aggregation framework in that case.
Example :
find( { _id: 1, "products._id": 4 }, { products: { $elemMatch: { _id: 4 } } } ).pretty()
I have followed model stored in mongoDB:
{
"_id" : "some_table_name",
"content" : [{
"id" : "1",
"locname" : "KKH"
}, {
"id" : "2",
"locname" : "Singapore National Eye Centre"
}]
}
I try to find criteria to update 2nd element (id=2) aka add new String.
"new_element" : "foo"
So new view should be:
{
"_id" : "some_table_name",
"content" : [{
"id" : "1",
"locname" : "KKH"
}, {
"id" : "2",
"locname" : "Singapore National Eye Centre"
"new_element" : "foo"
}]
}
Form PHP
When I try to find 2nd node by id I use:
$array = $collection_bios2->findOne(
array("_id" => "some_table_name", "content.id" => "2"),
array("_id" => 0, "content.$" => 1)
);
But when I try to update it, new node enters under content:
$newdata = array('$set' => array("new_element" => "foo"));
$collection_bios2->update(
array("_id" => "some_table_name", "content.id" => "2"),
$newdata
);
I get:
{
"_id" : "some_table_name",
"content" : [{
"id" : "1",
"locname" : "KKH"
}, {
"id" : "2",
"locname" : "Singapore National Eye Centre"
}],
"new_element" : "foo"
}
Whats wrong in my implementation?
Please, help,
Maxim
You need to use the positional operator here:
array('$set'=>array('content.$.new_element':'foo'))
You can read more about it here: http://docs.mongodb.org/manual/reference/operator/positional/
How to search value in multidimensional array,
for example I want to search example keyword in the following data in mongodb
I used to fetch all data from command
>db.info.find()
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 3,
"name" : "XYZ",
"email" : "xyz#demo.com"
},
{
"sno" : 4,
"name" : "ABC",
"email" : "abc#demo.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
Now, to find data having example I used command
>db.info.find({"info.email":"example"})
and it gives
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 3,
"name" : "XYZ",
"email" : "xyz#demo.com"
},
{
"sno" : 4,
"name" : "ABC",
"email" : "abc#demo.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
But I want only 3 out of 5 sub rows like
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
Rohan, MongoDB always returns the whole document that you are searching on. You can't just make it return the array elements in which your keyword was found. If you want to do that, then you need to make sure all all embedded documents in the "info" field are in their own collection. And that might mean that you need to link them back to the original document in your "info" collection. Perhaps something like:
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
"info_id" : "12345",
},
Alternatively, you can of course do post-processing in PHP to obtain only the rows that you want.
Perhaps this is a good idea?
http://php.net/manual/en/class.mongoregex.php
I tried Map Reduce Function and it works on this type of problems the code is something like that:
Write a map function
map=function ()
{
filter = [];
this.info.forEach(function (s) {if (/example/.test(s.email)) {filter.push(s);}});
emit(this._id, {info:filter});
}
Write a reduce function
reduce=function(key, values) { return values;}
MapReduce Function
res=db.info.mapReduce(map,reduce,{out:{inline:1}})
And The Output look likes:
"results" : [
{
"_id" : ObjectId("4f9a2de0ea4a65c3ab85a9d3"),
"value" : {
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
}
],
"timeMillis" : 1,
"counts" : {
"input" : 3,
"emit" : 3,
"reduce" : 0,
"output" : 3
},
"ok" : 1,
Now you can find your search data from
printjson(res.results)
Did you try $ (projection)?
db.info.find({"info.email":"example"}, {"info.email.$":1})
document