0

I write this script at Robo 3T for mongodb:

db.Events.aggregate([
    {$match : {BusinessCode: /(([1-2]?[0-9])-([0-9]*)-([0-9]*)-([0-9]*)-([0-9]*)-([0-9]*)-([0-9]*))/}},
    {$project : {BusinessCode : {$arrayElemAt:[{$split : ["$BusinessCode", "-"]},0]}}},
    {$addFields: {"Domain":  "$BusinessCode"}},
    //{ $out : "Events" } 
],{
allowDiskUse: true
})

Now I want instead of addFields , I use update! how could I do that?

as you can see I used out but I do not want to create new collection or do replace my collection, I want update aggregate return into my collection.

in my scenario Domain if exist, filled with new value.

I have seen some post that it was for 4 years ago like this

Now it is possible?

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149

1 Answers1

0

finally I find the answer:

this is not my answer. (chridam answer)

var pipeline = [
    {
        "$match": { "date_order": { "$gt": v_date1year } }
    },
    {
        "$group": {
            "_id": "$id_client", 
            "count": { "$sum" : 1 }
        }
    },
    { "$out": "tmp_indicators" }        
];
db.orders.aggregate(pipeline);

var bulkOps = db.tmp_indicators.find().map(function (doc) { 
    return { 
        "updateOne": { 
            "filter": { "_id": doc._id } ,              
            "update": { "$set": { "nb_orders_1year": doc.count } } 
        }         
    };
});

db.clients.bulkWrite(bulkOps, { "ordered": true });

complete answer is here

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149