I have a little problem, hope You can help me.
I have next array structure in MongoDB:
{
"_id":ObjectId("4e43cf96e7c7914b87d2e0ff"),
"list" :[
{
"id" : ObjectId("4e43cf96e62883ee06000002"),
"user_name" : "login",
"comments" : [ ] //insert here new data
},
{
"id" : ObjectId("4e43cf96e62883ee06000003"),
"user_name" : "login",
"comments" : [ ]
}
]
}
And I want to insert new data to comments by "list.id". But I try like that:
$post_id = "4e43cf96e62883ee06000002";
$this->connection->user->feed->update(
array ('list.id' => new MongoId($post_id) ),
array ('$push' => array ('list'=>array('comments'=>$data ) ))
)
But that code create new field in structure, but not add data to field 'comments':
{
"_id":ObjectId("4e43cf96e7c7914b87d2e0ff"),
"list" :[
{
"id" : ObjectId("4e43cf96e62883ee06000002"),
"user_name" : "login",
"comments" : [ ] //insert here new data
},
{
"id" : ObjectId("4e43cf96e62883ee06000003"),
"user_name" : "login",
"comments" : [ ]
},
{
"comments" : { //added new field
//there is my data
}
]
}
Also I tried:
$this->connection->user->feed->update(
array ('list.id' => new MongoId($post_id) ),
array ('$push' => array ('list.comments'=>$data ) )
);
But nothing.
This works fine in mongo console :)
db.yar.update({"list.id":ObjectId("4e43cf96e62883ee06000002")}, {"$addToSet":{"list.$.comments":"my cool comment"}});
Use $addToSet modifier, because comments is a array not field.
Related
MongoDB schema:
{
"_id" : ObjectId("60057ff5c34000008a00214a"),
"Name" : "Test Store",
"AFM" : "099360666",
"DOY" : "ffa",
"Address" : "faf",
"TEL" : "sss",
"Products" : [
{
"_id" : ObjectId("600584b8c34000008a00214d"),
"pID" : "099360666/1",
"pNAME" : "old scholl 2",
"pPRICE" : "55",
"pBRAND" : "vans",
"pDESCRIPTION" : "eco frinedly",
"pSIZE" : "45,44",
"pDEPARTMENT" : "Men/Shoes/Trainers",
"pTHUMBNAIL" : "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg",
"pQUANTITY" : "7",
"pCOMMENTS" : {
"Comment" : [ ]
}
}
],
"nextProductCounter" : 2
}
I want to add comments through a html form using php to add comment at db.
The best I've got is this, but seems to not work
$new_comm = array(
"username" => "guest",
"date" => new \MongoDB\BSON\UTCDateTime(strtotime(date('d-m-Y 00:00'))),
"text" => "excellent product",
"rating" => "4"
);
$collection->updateOne(
array("Products._id" => new MongoDB\BSON\ObjectId("60058012c34000008a00214b")),
array('$push' => array("Products.pCOMMENTS" => array("Comment"=>$new_comm)))
);
Why the pCOMMENTS array isn't updating?
Expected results
"pCOMMENTS" : {
"Comment" : {
"username" : "guest",
"date" : "18/1/2021",
"text" : "excellent product",
"rating" : "4"
}
}
You need to use positional operator
Reference
$collection->updateOne(
array("Products._id" => new MongoDB\BSON\ObjectId("60058012c34000008a00214b")),
array('$push' => array("Products.$.pCOMMENTS" => array("Comment"=>$new_comm)))
);
i have document like this
{
"_id" : ObjectId("512e28984815cbfcb21646a7"),
"primary" : [
"0":{
'abc',
'xyz'
}
]
},
{
"_id" : ObjectId("512e2898481asdf7asdf7"),
"primary" : [
"0":{
'xyz',
'ght',
'aaa'
}
]
}
here i want to get all records that contain 'xyz' in primary.0 array
also tried laravel whereIn and aggregate $in but no luck..
please suggest me solution in laravel or mongodb aggregate..
If I have the following schema:
users = {
_id: MongoID
name: String,
email: String,
password: String,
online: Boolean, // Tells if user is online or not
friends: Array, // Storing _ids of friends
request: Array
}
Now, I want to get the name of all the friends which are online, sorted by name.
MongoDB database:
{
"_id" : ObjectId("572e43d71ccbdd080f00002b"),
"name" : "Bot 3",
"email" : "bot3#gmail.com",
"password" : "202cb962ac59075b964b07152d234b70",
"online" : false,
"friends" : [
ObjectId("572efe481ccbdd9c12000029")
],
"requests" : []
}
{
"_id" : ObjectId("572efe481ccbdd9c12000029"),
"name" : "Bot 4",
"email" : "bot4#gmail.com",
"password" : "202cb962ac59075b964b07152d234b70",
"online" : false,
"friends" : [
ObjectId("573074e81ccbddc816000029"),
ObjectId("572e43d71ccbdd080f00002b")
],
"requests" : [ ]
}
{
"_id" : ObjectId("573074e81ccbddc816000029"),
"name" : "Bot 5",
"email" : "bot5#gmail.com",
"password" : "202cb962ac59075b964b07152d234b70",
"online" : true,
"friends" : [
ObjectId("572efe481ccbdd9c12000029")
],
"requests" : [ ]
}
I want some query like:
var arr = db.users.find({"_id": ObjectId("572efe481ccbdd9c12000029")},
{"friends": 1, "_id": 0});
db.users.find({$in: arr["friends"]}, {"name": 1, "online": 1}).sort({"name": 1});
I tried this php code on the above example (not even close to requirement):
<?php
$cursor1 = $users->find(
array(
"_id" => new MongoID($id),
),
array(
"friends" => 1
)
);
$cursor1->next();
$doc1 = $cursor1->current();
$friend_arr = $doc1['friends'];
foreach($friend_arr as $f_id){
$cursor2 = $users->find(
array("_id" => new MongoID($f_id)),
array("name"=>1));
$cursor2->next();
$doc2 = $cursor2->current();
echo "<div>".$doc2['name']."</div>";
}
?>
But I know it does not satisfy the needs. That's why I am asking for help.
I am new to MongoDB with php.
Thank you so much in advance for any help.
Ok guys, I got the solution after some thinking.
$cursor1 = $users->find(
array(
"_id" => new MongoID($id),
),
array(
"friends" => 1
)
);
$cursor1->next();
$doc1 = $cursor1->current();
$friend_arr = $doc1['friends'];
$cursor2 = $users->find(
array(
"_id"=> array(
'$in' => $friend_arr
)
),
array(
"name" => 1
)
);
$cursor2->sort(array("name"=>1));
foreach($cursor2 as $doc2){
echo "<div>".$doc2['name']."</div>";
}
This code works properly. I tested it before posting.
{
"__v" : 2,
"_id" : ObjectId("54dc4fd10ac1f6a066a0646c"),
"desc" : "test1",
"lists" : [
{
"index" : 1,
"text" : "__point",
"_id" : ObjectId("54dc4fda0ac1f6a066a0646d"),
"createdAt" : ISODate("2015-02-12T07:01:46.390Z")
},
{
"index" : 2,
"text" : "__point",
"_id" : ObjectId("54dc4fdd0ac1f6a066a0646e"),
"createdAt" : ISODate("2015-02-12T07:01:49.668Z")
}
],
"name" : "test" }
The test array looks like this. i want to add 'category' field and add value '0' in lists checking _id.
i tried
$collection->update(array('_id' => new MongoId("54dc4fd10ac1f6a066a0646c")),array('$set' => array(new MongoId("54dc4fda0ac1f6a066a0646d").".lists.category" => '0')));
like this but it doesn't work.
set third parameter of update to true.
i think there is some problem in finding which embedded document for updated.
try this one might be work.
$collection->update(array('_id' => new MongoId("54dc4fd10ac1f6a066a0646c"), 'lists._id' => "54dc4fda0ac1f6a066a0646d"), array('$set' => array("lists.$.category" => '0')), true);
Hi I don't know more about PHP but you can do this from following script
db.collectionName.find({
"lists": {
"$exists": true
}
}).forEach(function(data){
for(vari=0;i<data.lists.length;i++){
db.collectionName.update({
"_id": data._id,
"lists.index": data.lists[
i
].index
},
{
"$set": {
"lists.$.category": 0
}
},
true,
true);
}
})
This has to be easy, but i can't seem to figure it out... Let say i had a collection users and this is the first item in the collection:
{
"_id" : ObjectId("4d8653c027d02a6437bc89ca"),
"name" : "Oscar Godson",
"email" : "oscargodson#xxx.com",
"password" : "xxxxxx",
"tasks" : [
{
"task" : "pick up stuff",
"comment" : "the one stuff over there"
},
{
"task" : "do more stuff",
"comment" : "lots and lots of stuff"
}
] }
How with the PHP driver for MongoDB would I then go and add another "task" to the "tasks" array in this item in a collection
Use Mongo's $push operation:
$new_task = array(
"task" => "do even more stuff",
"comment" => "this is the new task added"
);
$collection->update(
array("_id" => ObjectId("4d8653c027d02a6437bc89ca")),
array('$push' => array("tasks" => $new_task))
);