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'
)
)
))
Related
i have got a mongo array , in which i want to remove the whole block , if the nested array of that block is empty
I have attached the array below
{
"_id" : ObjectId("5b17b991c440782b5a218cd1"),
"vendor_view_id" : 741733,
"product" : [
{
"id" : ObjectId("5b86546540c1c414543e4333"),
"vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
"product_type_id" : ObjectId("5ae8348b7ae0d9538e45ab46"),
"condition_id" : [ ],
"shipping_cost" : 100,
"date_added" : "2018-08-29-08-08-05",
"date_status_change" : "2018-08-29-08-08-05",
"status" : 0
},
{
"id" : ObjectId("5b8654ba40c1c4145d1f5473"),
"vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
"product_type_id" : ObjectId("5ae834b17ae0d9538e45ab48"),
"condition_id" : [ ],
"shipping_cost" : 100,
"date_added" : "2018-08-29-08-09-30",
"date_status_change" : "2018-08-29-08-09-30",
"status" : 0
},
{
"id" : ObjectId("5b8655a840c1c415080b0a33"),
"vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
"product_type_id" : ObjectId("5ae834a67ae0d9538e45ab47"),
"condition_id" : [
{
"_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
"status" : 0,
"date_added" : "2018-08-29-08-13-28"
}
],
"shipping_cost" : 100,
"date_added" : "2018-08-29-08-13-28",
"date_status_change" : "2018-08-29-08-13-28",
"status" : 0
}
]
}
I would like to delete the array block where product.condition_id is empty
So far i have tried this
$this->collection_name->collection->updateOne([
'_id' => $vendor_id,
],
[
'$unset' =>
[
'product.$.condition_id' =>
[
'$size'=>0,
]
]
])
EDIT 1:
db.collection_name.collection({_id : ObjectId('5b17b991c440782b5a218cd1'),
"product.condition_id.$":{ "$exists": false }},
{ "$unset": { "product.$": "" }});
still not working
db.collection_name.update(
{},
{$pull : {product : {condition_id : {$size : 0} }}},
{ multi: true } // multi : true will updates multiple documents that meet the query criteria
)
if condition_id array is empty pull the document.
Output:
{
"_id" : ObjectId("5b17b991c440782b5a218cd1"),
"vendor_view_id" : 741733,
"product" : [
{
"id" : ObjectId("5b8655a840c1c415080b0a33"),
"vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
"product_type_id" : ObjectId("5ae834a67ae0d9538e45ab47"),
"condition_id" : [
{
"_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
"status" : 0,
"date_added" : "2018-08-29-08-13-28"
}
],
"shipping_cost" : 100,
"date_added" : "2018-08-29-08-13-28",
"date_status_change" : "2018-08-29-08-13-28",
"status" : 0
}
]
}
You can transform your json to PHP array json_decode($json) so you'll be able to process it before it turn into json after.
json_encode($arr)
Note that you can check the depth of the array in your case to see if product.condition_id is empty.
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);
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,
)),
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)
)
);
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