0

Below query works fine for mongo version 4.2, but when I run the same query on the older version it is an error -"The dollar ($) prefixed field $concat in GUID.$concat is not valid for storage".

db.getCollection('xxxx').update(
    { "SHARE": false },
    [{ 
        "$set": { 
            "GUID": { 
                "$concat": [
                    { $toString: "$ID" }, 
                    { $toString: "$DATE" }
                ] 
            } 
        } 
    }],
    { 
        upsert: false,
        multi: true 
    }
)

Is there any way to modify the above query in older mongo version.

5
  • 1
    yes this is from 4.2, you can not use in older version. update-documents-with-aggregation-pipeline Commented Aug 11, 2020 at 14:00
  • can that help your case: stackoverflow.com/questions/52326901/…? Commented Aug 11, 2020 at 16:36
  • With older versions (pre MongoDB 4.2) you can update using two operations - first the aggregation (using $concat, etc.) and then an update. Commented Aug 12, 2020 at 1:41
  • Your update query will update documents which are the result of the aggregation. Check your aggregation result, first and then update. Commented Aug 12, 2020 at 7:42
  • @prasad_ Thanks for the help. below query works for me. db.getCollection('xxxx').aggregate([ {$addFields: { GUID: { $concat: [ {$toString:"$ID"} ] }}} ]).forEach ( function(x) { db.getCollection('xxxx').update({"_id":x._id},{$set:{GUID:x.GUID}},{multi:true}); //print((x._id.toString())) } ) Commented Aug 12, 2020 at 8:43

1 Answer 1

0

Below query works for me,

db.getCollection('xxxx').aggregate([ {$addFields: { GUID: { $concat: [ {$toString:"$ID"} ] }}} ]).forEach ( function(x) { db.getCollection('xxxx').update({"_id":x._id},{$set:{GUID:x.GUID}},{multi:true}); //print((x._id.toString())) } )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.