Is it possible to do a $lookup aggregation between two databases in Mongodb?

Is it possible to do a $lookup aggregation between two databases in Mongodb? It is not possible to query using lookup in two different db’s. $lookup in mongodb supports Performs a left outer join to an unsharded collection in the same database. { $lookup: { from: <collection to join>, localField: <field from the input documents>, … Read more

Using Mongoose / MongoDB $addToSet functionality on array of objects

You need to use the $ne operator. var data = { ‘kind’: ‘tortoise’, ‘hashtag’: ‘foo’ }; Model.update( { ‘articles.hashtag’: { ‘$ne’: ‘foo’ } }, { ‘$addToSet’: { ‘articles’: data } } ) This will update the document only if there is no sub document in the “article” array with the value of hashtag equals to … 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

mongodb impossible (?) E11000 duplicate key error dup key when upserting

I am afraid that this is an ongoing problem. I had the same problem and I found a jira ticket about this: https://jira.mongodb.org/browse/SERVER-14322 It is possible that two updates come in with upsert:true, resulting in neither finding a document and both inserting new documents which conflict on unique index violations of the query predicate. The … Read more

Loop through all Mongo collections and execute query

There is the db.getCollectionNames() helper method that does this for you. You can then implement your code: db.getCollectionNames().forEach(function(collname) { // find the last item in a collection var last_element = db[collname].find().sort({_id:-1}).limit(1); // check that it’s not empty if (last_element.hasNext()) { // print its timestamp printjson(last_element.next()._id.getTimestamp()); } }) You probably also want a .hasNext() check in … Read more

Append item to MongoDB document array in PyMongo without re-insertion

You don’t need to use to retrieve the document first just use the .update method with the $push operator. def update_tags(ref, new_tag): coll.update({‘ref’: ref}, {‘$push’: {‘tags’: new_tag}}) Since update is deprecated you should use the find_one_and_update or the update_one method if you are using pymongo 2.9 or newer

$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” }} ])

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