Aggregration join not working in mongo db - mongodb

I have three collections with following feilds
productTbl
-- productId
-- productName
-- categoryId
-- vendorId
.... so on
categoryTbl
-- categoryId
-- categoryName
VendorTbl
-- VendorID
-- VendorName
I have written below mongodb aggregrate lookup query
$pipeline = array(
array(
'$lookup' => array(
'from' => 'VendorTbl',
'localField' => 'vendorId',
'foreignField' => 'VendorID',
'as' => 'vendordetails'
)
),
array(
'$lookup' => array(
'from' => 'categoryTbl',
'localField' => 'categoryId',
'foreignField' => 'categoryId',
'as' => 'categorydetails'
)
),
);
$output = $this->db->productTbl->aggregate($pipeline);
Now the above query is fetching all the feilds from product table but categorydetails and vendordetails are empty. I am not able to trace this issue.
Please help!!!
The output is like
{"$id":"59941ea11d78596801000029"},"productId":14,"productName":"Pencils","vendorId":"2","categoryId":"5","quantity":"122","sellingPrice":"122","vendordetails":[],"categorydetails":[]
The actual document for category table is like
{
"categoryId": 18,
"categoryName": "swdgvqaedeadsgsgdf",
}
The actual document for vendor table is like
{
"_id": ObjectId("599413e01d78596409000029"),
"VendorID": 3,
"VendorName": "hydfyugjgfu",
}
The actual document for product table is like
{
"_id": ObjectId("59941ec11d7859680100002a"),
"productId": 15,
"productName": "Pens",
"vendorId": "3",
"categoryId": "7",
...
...
}

Related

How to recreate perl's array of hashes in golang?

In perl I can push $hashref into #array and have this data for next foreach and possible encode_json (HTTP POST).
I can't figure out how to recreate the same login in golang?
$VAR1 = [
{
'address' => 'test.com',
'id' => 101,
'hostgroups' => [
zero
'one',
'or many'
],
'host_name' => 'test.com',
'alias' => 'test.com',
'template' => 'generic',
'file_id' => 'etc/config'
},
{
'address' => 'test2.com',
'id' => 102,
'hostgroups' => [
zero
'one',
'or many'
],
'host_name' => 'test2.com',
'alias' => 'test2.com',
'template' => 'generic',
'file_id' => 'etc/config'
},
(..)
var array = []map[string]interface{}{
{"address": "test.com", "hostgroups": []string{"zero", "one", "or many"}, "id": 101},
{"address": "test2.com", "hostgroups": []string{"zero", "one", "or many"}, "id": 102},
}
This is the answer.
type host map[string]interface{}
var hosts []host
h := host{
"id": id,
"file_id": "etc/config/hosts.cfg",
"host_name": host_name,
"alias": host_name,
"address": host_name,
"hostgroups": hg,
"template": "generic-host",
}
hosts = append(hosts, h)

$match query not working for lookup array in mongodb

I have two tables in mongodb database
activityCountTbl contains data like
{
"_id": ObjectId("5c234f7e3250041280000ca3"),
"activityId": ObjectId("5c0e27ee590a06bf08003c33"),
"weekgroupId": ObjectId("5bfbddbcbb2c5645f8001495"),
"squadronId": ObjectId("5bfc7b7ebb2c56c320002a0a"),
"attendingCount": NumberInt(6),
....
}
{
"_id": ObjectId("5c234f7e3250041280000ca3"),
"activityId": ObjectId("5c0e27ee590a06bf08003c33"),
"weekgroupId": ObjectId("5bfbddbcbb2c5645f8001496"),
"squadronId": ObjectId("5bfc7b7ebb2c56c320002a0a"),
"attendingCount": NumberInt(6),
....
}
squadronTbl contains data like
{
"_id": ObjectId("5c19ccb7590a060691000554"),
"squadronCode": "336",
"squadronName": "336TRS",
}
{
"_id": ObjectId("5c19ccb7590a060691000556"),
"squadronCode": "337",
"squadronName": "337TRS",
}
I am storing count details of a particular activity of a weekgroup in activityCountTbl. I am performing lookup on squadronTbl with activityCountTbl
for fetching squadrons details of a particular weekgroup. The below code is not working.
When I comment/delete the $query code, it fetches all the squadrons of all weekgroups.
$query = ["ActivityArray.weekgroupId" => new MongoDB\BSON\ObjectID("5bfbddbcbb2c5645f8001495"), "ActivityArray.funRun" => "Yes"];
$pipeline = array(
[
'$match' => $query
],
[
'$lookup' => [
'from' => 'activityCountTbl',
'localField' => '_id',
'foreignField' => 'squadronId',
'as' => 'ActivityArray'
]
],
['$project' => [
'_id' => 1.0,
'squadronName' => 1.0,
'ActivityArray' => 1.0
]],
);
return $this->db->squadronTbl->aggregate($pipeline)->toArray();
Please help !!!
$query = ["ActivityArray.weekgroupId" => new MongoDB\BSON\ObjectID("5bfbddbcbb2c5645f8001495"), "ActivityArray.funRun" => "Yes"]
$pipeline = array(
[
'$match' => []
],
[
'$lookup' => [
'from' => 'activityCountTbl',
'localField' => '_id',
'foreignField' => 'squadronId',
'as' => 'ActivityArray'
]
],
[
'$match' => $query
],
['$project' => [
'_id' => 1.0,
'squadronName' => 1.0,
'ActivityArray' => 1.0
]],
);
something like that

MongoDB query $lookup with prefix

I have two collection rounds and summaries
A record in rounds looks like
{
"_id": "2018-04",
"name": "Round 2018-04"
}
A record in summaries look like
{
"phase": "round:2018-04",
"userId": NumberLong(66325),
}
I want to query summaries and lookup into rounds joining based on phase of summaries into _id of rounds
PROBLEM: It will be pretty simple if there was no prefix of round: in phase.
Is there anyway to do this?
This is my code so far.
$cursor = $this->mongo->selectCollection('summaries')->aggregate([
array('$match' => []),
array(
'$lookup' => [
'from' => 'rounds',
'localField' => 'phase',
'foreignField' => '_id',
'as' => 'roundDetail'
]
),
array(
'$unwind' => '$roundDetail',
),
array(
'$project' => [
'userId' => 1,
'roundDetail.name' => 1
]
)
]);
MongoDB version 3.4.16
You can use substrBytes to remove characters from the string.
$cursor = $this->mongo->selectCollection('summaries')->aggregate([
array('$match' => []),
array('$addFields' => [ 'phase' => [ '$substrBytes' => [ '$phase', 6, 7 ] ] ] ),
array(
'$lookup' => [
'from' => 'rounds',
'localField' => 'phase',
'foreignField' => '_id',
'as' => 'roundDetail'
]
),
array(
'$unwind' => '$roundDetail',
),
array(
'$project' => [
'userId' => 1,
'roundDetail.name' => 1
]
)
])

Find average time between multiple datetime objects mongodb

I want to find the average time a client visit at particular location. My data structure is as follows:
{
"_id": ObjectId("5aea9f9a83b391f80e00a1b1"),
"Client": "ABC",
"Timestamp": "2018-05-03 10:47:42"
},
{
"_id": ObjectId("5aea9f9a83b391f80e00c1a1"),
"Client": "ABC",
"Timestamp": "2018-05-03 11:05:04"
} ,
{
"_id": ObjectId("5aea9f9a83b391f80e00a1e1"),
"Client": "ABC",
"Timestamp": "2018-05-03 13:05:04"
} ,
{
"_id": ObjectId("5aea9f9a83b391f80eaea1e1"),
"Client": "DEF",
"Timestamp": "2018-05-03 11:20:44"
}
I have just tried code so far in codeigniter
$optn = array(
array('$group' => array( '_id' => '$Client', 'date1' => array('$min' => '$Timestamp'), 'date2' => array('$max' => '$Timestamp'), 'avg_time' => array('$avg' => array('$subtract' => array('$date2', '$date1')))))
);
$repeatdata = $this->mongo_db->aggregate("logsdata", $optn);
echo '<pre>'; print_r($repeatdata);
It gives me result as:
Array
(
[waitedMS] => 0
[result] => Array
(
[0] => Array
(
[_id] => ABC
[date1] => 2018-05-03 10:47:42
[date2] => 2018-05-03 13:05:04
[avg_time] =>
)
[1] => Array
(
[_id] => DEF
[date1] => 2018-05-03 11:20:44
[date2] => 2018-05-03 11:20:44
[avg_time] =>
)
)
)

Query in mongo with $in operator

I have one collection which contain documents with following structure:
array (
'_id' => new MongoId("54369e2904f7055598463483"),
'user' => '123',
'model' =>
array (
'test' =>
array (
'0' => 'test1',
'1' => 'test2',
'2' => 'test3',
),
),
)
And if I do the next query in Rock Mongo I got the results.
array(
'user' => array(
'$in' => array(
'0' => '123'
)
)
)
But if I try to fetch documents which have for example key => pair value '0' => 'test1' & '1' => 'test2' I don't get any data with the next query:
array(
'model' => array(
'test' => array(
'$in' => array(
'0' => 'test1',
'1' => 'test2',
)
)
)
)
Where I'm making mistake?
Thanks in advance
PHP's array/hash mixup makes your example a little hard to read and reason about. But here's what your model looks like in Mongo's native syntax (a language which makes a distinction between lists and maps):
{
"_id": ObjectId(...),
"user": 123,
"model": {"test": ["test1", "test2", "test3"]}
}
So you want to query on model.test (nested document). Using Mongo's native syntax, you'd do the following:
collection.find({"model.test": {"$in": ["test1", "test2"]})
See http://docs.mongodb.org/manual/reference/method/db.collection.find/ for more info.