where condition db array contain value Laravel Mongodb - php

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..

Related

conversion to PHP, update and insert in subdocument

I am having trouble converting the statement to php
Here is the structure of the data:
db.rooms.find({"_id" : "1620888265"}).pretty();
{
"_id" : "1620888265",
"start" : 1620988265,
"end" : null,
"users" : [
{
"name" : "owner",
"score" : 0.9,
"singin" : 1620895469
},
{
"name" : "user",
"singin" : 1620895769
}
],
"questions" : [
{
"id": 1
"title" : "X",
"released" : false
}
]
}
Here the command that works for me:
db.rooms.updateOne({"_id" : "1620970325obn","questions.ident":2},{$set : {"questions.$.released":true,"questions.$.time":'1415'}});
Once translated to PHP (not working for me):
$rooms_db->updateOne(array("_id" => $room,"questions.ident"=>$ident), array ('$set'=>array('questions.$.released' => true,'questions.$.time'=>$time)));
The problem has been that the variable "$ident" was a number, but for some reason PHP treated it as a String. This can be seen with var_dump ($ident) .
"questions.ident"=>(int)$ident), this fixed the problem

Sorting array/sub document in mongodb PHP based on datetime [duplicate]

This question already has answers here:
Mongodb and sorting sub array
(2 answers)
Closed 5 years ago.
I am stuck at a point where i need to sort the documents based on the date time field following is my database structure
"_id" : ObjectId("59199bbb05b505777d86e9a2"),
"Email" : "sk.sagarkhan95#gmail.com",
"BlogPosts" : [
{
"PostID" : 7,
"Title" : "Party Time",
"Name" : "Sagar Khan",
"Description" : "Farewell party with 2k17 batchmates at atmosphere 4... #AllNight #Fun #CHEERS....",
"Date" : ISODate("2017-05-15T13:18:01Z"),
"Image" : "users/sk.sagarkhan95#gmail.com/pictures/prof-pic.jpg",
"Likes" : [
"hs#gmail.com",
"shweta#gmail.com",
"ankita#gmail.com",
"sk.sagarkhan95#gmail.com"
],
"Comments" : [
{
"CommentID" : 4,
"email" : "hs#gmail.com",
"Name" : "Harish Shinde",
"Image" : "users/hs#gmail.com/pictures/prof-pic.jpg",
"comment" : "Yo... Cheers ",
"Time" : ISODate("2017-05-15T13:18:40Z")
}
]
}
]}
{
"_id" : ObjectId("59450ce02aa01e3027df57be"),
"Email" : "hs#gmail.com",
"BlogPosts" : [
{
"PostID" : 2,
"Title" : "At Goa",
"Name" : "Harish Shinde",
"Description" : "Relaxing at baga Beach Goa ",
"Date" : ISODate("2017-06-17T11:05:20Z"),
"Image" : "users/hs#gmail.com/pictures/prof-pic.jpg",
"Likes" : [
"sk.sagarkhan95#gmail.com"
],
"Comments" : [ ]
}
]
}
Now i want to sort the documents in descending order based on Date in the BlogPosts array.
In Mongodb Console i tried
db.Timeline.find().sort({BlogPosts.Date : -1 }).pretty()
But In PHP i am not able to do this
I tried
$cursor = $collection->find()->sort(array("BlogPosts"=>array("Date"=> -1)));
and
$cursor = $collection->find()->sort(array("BlogPosts.$.Date"=> -1 ));
I also tried the solution mentioned in this answer but no luck... Please help me out
Instead of:
sort(array("BlogPosts.$.Date"=> -1 ));
try
sort(array("BlogPosts.Date"=> -1 ));
Refer this question

MongoDB - PHP modifying array's object values

I have a field which stores an array of objects. I want to modify the object's "status" value depending on the "id" value the object possesses.
"authors" : [
{
"id" : "18",
"first_name" : "Buddhika",
"last_name" : "Chathuranga",
"$$hashKey" : "object:35",
"status" : NumberLong(0)
},
{
"id" : "3", // search for this number
"first_name" : "Pasindu",
"last_name" : "Priyanath",
"$$hashKey" : "object:43",
"status" : NumberLong(0) // modify this to 1
}
],
We can leverage positional update in MongoDB to update values inside array.
Mongo Positional Update
Please find script below.
db.authors.update(
{"authors.id": "3"},
{ $set: { "authors.$.status" : NumberLong(1) }}
)
Update in Mongo DB php:
db.collection.update( criteria, objNew, upsert, multi );
Detail Explanation

PHP MongoDB update doesn't work

{
"__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);
}
})

Insert data into inner array in MongoDB

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.

Categories