I am facing an issue while pushing the data in nested document using mongodb and laravel. The collection structure are as follows:
{
"_id" : ObjectId("56f432f5b720531878f8c935"),
"test1" : [{
"Name" : "test",
"_id" : ObjectId("56f432f5b720531878f85926"),
"isActive" : 0,
"test2" : [{
"Name" : "testing",
"_id" : ObjectId("56f432f5b720531858f8c965"),
"isActive" : 0,
"test3" : [{
"Name" : "test",
"_id" : ObjectId("56f432f5b720541858f8c965"),
"isActive" : 0
}, {
"Name" : "test1",
"_id" : ObjectId("56ffeb9c7ae65b1124000714")
}]
}]
}],
"created" : ISODate("2012-12-31T18:30:00Z")
}
I tried to solve the issue using aggregator but stood still in finding the index of the nested document. since i can push the data using
test1.0.test2.test3 likewise... in there any way to find the index of the nested document using mongodb alone?
array('$unwind' => '$test1'),
array('$unwind' => '$test1._id'),
array('$unwind' => '$test1.test2'),
array('$unwind' => '$test1.test2._id'),
array(
'$match' => array(
'$and' => array(
array('test1._id' => array('$in' => array('234234234'))),
array('test1.test2._id' => array('$in' => array('456456456'))),
)
)
),
array('$project' => array(
'test1' => 1,
)),
Related
When the below query is run the results are displayed in mongo shell.
Sample Records
{
"_id" : ObjectId("587e21df6e79d255011a9c6a"),
"vendor_id" : "101",
"subscription_id" : 14,
"created_at" : ISODate("2017-01-17T13:53:35.272Z")
}
{
"_id" : ObjectId("587e21df6e79d255011a9c6c"),
"vendor_id" : "102",
"subscription_id" : 14,
"created_at" : ISODate("2017-01-17T13:56:35.272Z")
}
Query
db.user_config.aggregate({$group:
{
_id : "$subscription_id",
list:
{
$push:
{
_id: "$_id",
vendor_id: "$vendor_id"
}
}
}})
Results
/* 1 */
{
"result" : [
{
"_id" : 14,
"list" : [
{
"_id" : ObjectId("587e21df6e79d255011a9c6a"),
"vendor_id" : "user_101"
},
{
"_id" : ObjectId("587e21df6e79d255011a9c6b"),
"vendor_id" : "user_101"
}
]
}
],
"ok" : 1.0000000000000000
}
But when same thing is done through laravel the following error occurs. $pipeline is not a list (unexpected index: "$group")
Below is the laravel code
$configuration_list = UserConfig::raw()->aggregate([
'$group' => [
'_id' => '$subscription_id',
'list' => ['$push' =>
['_id' => '$_id', 'vendor_id' => '$vendor_id']]
]
]
);
Can someone please help me solve this issue..
Wrap the aggregation pipeline steps in an array:
$pipeline = [
[
'$group' => [
'_id' => '$subscription_id',
'list' => [
'$push' => [
'_id' => '$_id',
'vendor_id' => '$vendor_id'
]
]
]
]
];
$configuration_list = UserConfig::raw()->aggregate($pipeline);
My aim is to create inner array to main array when search for userid as 1
Below is my data
{ "_id" : 2,
"name" : "test1",
"data" :[{"_id" : "1","file" : "nic", "userid" : [1,2 ]},
{"_id" : "2","file" : "nic1","userid" : [1 ] },
{"_id" : 3,"file" : "nick2","userid" : [1,2 ]}
]},
{ "_id" : 3,
"name" : "test2",
"data" : [{"_id" : "1","file" : "nic","userid" : [1,2 ] },
{"_id" : "2","file" : "nic1", "userid" : [3,2 ] }
]}
need to get out put as
{"_id" : 1,"file" : "nic", "userid" : [1,2 ],"main_name" : "test1","main_id" : 2},
{"_id" : 2,"file" : "nic1","userid" : [1 ] ,"main_name" : "test1","main_id" : 2 },
{"_id" : 3,"file" : "nick2","userid" : [1,2 ],"main_name" : "test2","main_id" : 3},
{"_id" : 1,"file" : "nic","userid" : [1,2 ] ,"main_name" : "test2" ,"main_id" : 3}
Basically the same answer to your last question, but without the $group to reconstruct the array and use a $project instead to restructure the document from the already de-normalized array elements.
$collection->aggregate(array(
array( '$match' => array( "data.userid" => 1 )),
array( '$unwind' => '$data' ),
array( '$match' => array( 'data.userid' => 1 )),
array(
'$project' => array(
'_id' => '$data._id',
'nic' => '$data.nic',
'user_id' => '$data.user_id',
'main_name' => '$name',
'main_id' => '$_id'
)
)
))
I'm having Mongo structure like this,
"campaigns":[{
"summary" :
[ {
"postcode" : [ {
"id" : "71",
"name" : "Recall 1",
"qty" : 3,
"history" :
[ { "timestamp" : "2014-12-16 11:15:32",
{ "timestamp": "2014-12-16 11:15:53"
} ]
} ]
},
{
"postcode" :
[ {
"id" : "72",
"name" : "Recall 2",
"qty" : 1,
"history" : [ { "timestamp" : "2014-12-16 11:15:53" } ]
} ]
}]
I'm trying to i) increment qty of postcode.id : 72 ii) insert another timestamp for that same postcode id 72.
My code:
$collection->update(array("campaigns.task.id" => $b,"_id"=> new MongoId($objectid),"campaigns.0.summary.0.postcode.0.id" => $a), array('$inc' => array('campaigns.0.summary.0.postcode.0.qty' => 1)));
$collection->update(array("campaigns.task.id" => $b,"_id"=>new MongoId($objectid),"campaigns.0.summary.0.postcode.0.id" => $a),
array('$addToSet' => array('campaigns.0.summary.0.postcode.0.history' => array("timestamp"=>$now->format('Y-m-d H:i:s')))));
but postcode.id = 72 not gets updated, i'm confused with this nested subdocument can anyone suggest me the solution ?
I'm trying to build a query to search geospatial data in mongo using php-mongo library.
Object structure
{
"_id" : ObjectId("536ecef59bdc8977f1e9c06f"),
"location" : {
"type" : "Point",
"coordinates" : [
[
-74.220408,
40.727703
]
]
},
"bounds" : {
"type" : "Polygon",
"coordinates" : [
[
[
-74.221403,
40.728457
],
[
-74.219413,
40.728457
],
[
-74.219413,
40.726949
],
[
-74.221403,
40.726949
],
[
-74.221403,
40.728457
]
]
]
},
"tile" : {
"type" : "Polygon",
"coordinates" : [
[
[
-74.220498,
40.727772
],
[
-74.220318,
40.727772
],
[
-74.220318,
40.727634
],
[
-74.220498,
40.727634
],
[
-74.220498,
40.727772
]
]
]
},
"status" : "2",
"dev" : "0"
}
Indexes
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "atlantic.Analyzer_Marks"
},
{
"v" : 1,
"key" : {
"location.coordinates" : "2d"
},
"name" : "location.coordinates_2d",
"ns" : "atlantic.Analyzer_Marks"
},
{
"v" : 1,
"key" : {
"bounds" : "2dsphere"
},
"name" : "bounds_2dsphere",
"ns" : "atlantic.Analyzer_Marks",
"2dsphereIndexVersion" : 2
},
{
"v" : 1,
"key" : {
"tile" : "2dsphere"
},
"name" : "tile_2dsphere",
"ns" : "atlantic.Analyzer_Marks",
"2dsphereIndexVersion" : 2
}
]
$mongo = new MongoClient("mongodb://someserver.com:27017");
$marks = $mongo->selectDB('atlantic');
$q = array('bounds' => array(
'$geoWithin' => array(
'$geometry' => array(
'type' => 'Polygon',
'coordinates' => array(array(
array(40.125246,-74.327963),
array(40.125246,-74.325989),
array(40.123738,-74.325989),
array(40.123738,-74.327963),
array(40.125246,-74.327963)
))
)
)
)
);
$marks = new MongoCollection($marks,'Analyzer_Marks');
$marks = $marks->find($q);
//var_dump($marks);
$results = array();
if(!empty($marks)){
foreach($marks as $mark) {
$results[] = array(
"location" => $mark['location'],
"tile" => $mark['tile'],
"status" => $mark['status']
);
}
}
This is the error I get:
Fatal error: Uncaught exception 'MongoCursorException' with
message 'someserver.com:27017: Can't canonicalize query: BadValue bad
geo query' in /var/www/html/one-call/lib/somefile.php:97
MongoDB Version 2.6.1
I can reproduce that error. I think you are missing one more array call wrapping around the array of coordinates:
$mongo = new MongoClient("mongodb://someserver.com:27017");
$marks = $mongo->selectDB('atlantic');
$q = array('bounds' => array(
'$geoWithin' => array(
'$geometry' => array(
'type' => 'Polygon',
'coordinates' => array(
array(
array(40.125246,-74.327963),
array(40.125246,-74.325989),
array(40.123738,-74.325989),
array(40.123738,-74.327963),
array(40.125246,-74.327963)
)
)
)
)
)
);
after modifying the code I don't receive that error anymore
My Mongo collection document is as:
Array
(
[_id] => MongoId Object
(
[$id] => 52ddf885f786208bf58020df
)
[FirstName] => Aloma
[State] => AR
[Title] => AVP
[Zip] => 71953
[campaign_id] => Array
(
[0] => 52fba54fce798c441400002b
[1] => 52fba687ce798c441400002c
)
)
campaign_id is the array of mongoid's reference to other collection.
How can I get the documents with campaign_id = 52fba54fce798c441400002b
Here we have to search mongoid 52fba54fce798c441400002b in array of campaign_id.
You can use following code in mongoDB.
db.collection.find(campaign_id:[52fba54fce798c441400002b])
It will return the documents that contains [52fba54fce798c441400002b] as campaign_id value exactly.
For example :
{ "_id" : ObjectId("52fc4b93633d0c54e662fc75"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11 ] }
{ "_id" : ObjectId("52fc4b8d633d0c54e662fc74"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 12, 14 ] }
{ "_id" : ObjectId("52fc4b86633d0c54e662fc73"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11, 12, 14 ] }
{ "_id" : ObjectId("52fc4b7d633d0c54e662fc72"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11, 12 ] }
> db.scores.find({campaign_id:[11]})
{ "_id" : ObjectId("52fc4b93633d0c54e662fc75"), "fname" : "test1", "state" : "AR", "campaign_id" : [ 11 ] }
Same thing, you can implement in PHP using php mongo client.
I hope it solves your problem.