0

I am new at mongodb. this is my field into my collection at mongodb.

"BusinessCode" : "13-8-113944-15-1-0-0",
"ProcessId" : UUID("122b8301-d1d9-431c-a411-142cc169c8eb"),
"WorkItemId" : NumberLong(376935),
"WKT" : ""

I want split BusinessCode and put the first number into new field.for example I want split 13 and put it into other field called Domain.

"Domain" : "13"

according this link, seems I have to use update but I don't know who to write <query> for my scenario. thanks for helping.

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149
  • Possible duplicate of [Update MongoDB field using value of another field](https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field) – sidgate Jan 31 '18 at 05:39
  • Currently not supported.. multiple jira tickets opened for improvement https://jira.mongodb.org/browse/SERVER-11345 – sidgate Jan 31 '18 at 05:39
  • @sidgate no it is not the same.I want to `split string of field's value` and then do updating my document. – Cyrus the Great Jan 31 '18 at 06:10
  • you want to create new field from existing field, which is similar to the linked question. This feature is not supported as of now. The linked question has alternative, for instance, iterate and update one document at a time.. you can do splitting in your code for each document – sidgate Jan 31 '18 at 06:15
  • my main question is splitting .I want just first string before line(-). in my scenario 13.I write this `{ $project : {BusinessCode : {$split: [ "$BusinessCode" ,"- "]}}},` but it doesn't right. – Cyrus the Great Jan 31 '18 at 06:20
  • do you want to update the existing document with new `domain` field, or just want to project it? – sidgate Jan 31 '18 at 06:45
  • @sidgate I want to update the existing document with new domain field. – Cyrus the Great Jan 31 '18 at 06:46

1 Answers1

0

Figured I would answer this, odd it is not answered already:

db.collection.update( 
  { },  
  [ { $set : { Domain: { 
                  $arrayElemAt: [ { 
                            $split: [ "$BusinessCode" , "-" ] }, 0 ] 
                       } } } ] , 
  { multi: true } )