How to deal with the timezone issue when storing dates in utc using mongod?

Aside from the SERVER-6310 mentioned by Matt Johnson, one other workaround is to use the $project operator to add or subtract from the UTC time zone to “shift the time” into the correct local zone. Turns out you can add or subtract time in milliseconds. For example, assuming I have a Date field called orderTime. … Read more

Mongo find duplicates for entries for two or more fields

You can easily identify the duplicates by running the following aggregation pipeline operation: db.collection.aggregate([ { “$group”: { “_id”: { “foreing”: “$foreing”, “value”: “$value” }, “uniqueIds”: { “$addToSet”: “$_id” }, “count”: { “$sum”: 1 } } }, { “$match”: { “count”: { “$gt”: 1 } } } ]) The $group operator in the first step is … Read more

Multiple Counts with single query in mongodb

You can try below $facet aggregation $count aggregation will always give you the counts for only single matching ($match) condition. So you need to further divide your each count into multiple section and that’s what the $facet provides by processes multiple aggregation pipelines within a single stage on the same set of input documents. db.collection.aggregate([ … Read more

$project in $lookup mongodb

You can use $lookup 3.6 syntax to $project the fields inside the $lookup pipeline User.aggregate([ { “$lookup”: { “from”: “schedules”, “let”: { “id”: “$_id.phone” }, “pipeline”: [ { “$match”: { “$expr”: { “$eq”: [“$customer.phone”, “$$id”] }}}, { “$project”: { “scheduleStart”: 1, “scheduleEnd”: 1 }} ], “as”: “user_detail” }} ])

$match in $lookup result

With MongoDB 3.4, you can run an aggregation pipeline that uses the $addFields pipeline and a $filter operator to only return the Company array with elements that match the given condition. You can then wrap the $filter expression with the $arrayElemAt operator to return a single document which in essence incorporates the $unwind functionality by … Read more

Mongodb aggregation lookup with conditions

First of all, it is all_category_id, not category_id. Secondly, you don’t link articles – all documents will have exactly the same article_category array. Lastly, you probably want to filter out articles that don’t have matched category. The conditional pipeline should look more like this: db.article.aggregate([ { $match: { title: { $regex: /example/ } } }, … Read more

Exception: can’t convert from BSON type EOO to Date

You likely have one or more docs with a created_at value that’s not a BSON Date and you’ll need to fix that by converting those values to Date or removing them. You can find those docs with a $not query that uses the $type operator like: db.snippets.find({created_at: {$not: {$type: 9}}}) If the created_at values are … Read more

MongoDB Aggregation – match if value in array

You can use aggregation expression in regular query in 3.6 version. db.collection_name.find({“$expr”: {“$in”: [“$value”, “$array”]}}) Using Aggregation: You can use $match + $expr in current 3.6 version. db.collection_name.aggregate({“$match”: {“$expr”: {“$in”: [“$value”, “$array”]}}}) You can try $redact + $in expression in 3.4 version. db.collection_name.aggregate({ “$redact”: { “$cond”: [ { “$in”: [ “$value”, “$array” ] }, “$$KEEP”, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)