estou tentando fazer um update aqui no mongoshell e estou com dificuldades.
Possuo o seguinte json:
{
"_id" : ObjectId("56cc03c16f4e85f538ef79ae"),
"contact_id" : NumberLong(1000295524418),
"gender" : 1,
"phonetic_gender" : 1,
"first_name" : "LEANDRO",
"score" : 44,
"address" : [
{
"address_id" : NumberLong(2634224807),
"rank" : 201604.0,
"score" : 7.0,
"street_type" : "AV",
"street_title" : "DA",
"street" : "EMILIA DE CASTRO MARTINS",
"number" : 34.0,
"district" : "JARDIM BELA VISTA",
"city" : "GUARULHOS",
"state" : "SP",
"zip_code" : 7132470.0,
"create_date" : ISODate("2014-08-07T00:00:00.000Z"),
"update_date" : ISODate("2016-05-03T00:00:00.000Z")
},
{
"address_id" : NumberLong(2634735566),
"rank" : 201410,
"score" : 10,
"street_type" : "AV",
"street_title" : "DA",
"street" : "EMILIA DE CASTRO MARTINS",
"district" : "JARDIM BELA VISTA",
"city" : "GUARULHOS",
"state" : "SP",
"zip_code" : "07132470",
"create_date" : ISODate("2014-08-07T03:00:00.000Z"),
"update_date" : ISODate("2014-08-07T03:00:00.000Z")
}
]}
Preciso percorrer todos os meus documentos e atualizar o tipo do campo rank e score no array de address.
Veja o seguinte código que estou fazendo:
var total = 0
var skip = 0
var total_adress = db.company.count() - skip
var bulk = db.person.initializeUnorderedBulkOp()
var person = db.getCollection('Person').find( {$and:[ {"contact_id":1000295524418}).addOption(DBQuery.Option.noTimeout).forEach(
function(person){
var contact_id = person.contact_id.valueOf()
bulk.find(
{ contact_id: contact_id }
).update(
{
$set: {
"address.$.zip_code":"address.zip_code".toString(),
"address.$.rank": NumberInt("address.rank"),
"address.$.number": "address.number".toString(),
"address.$.score": NumberInt("address.score") - 2
}
}
);
if((++total % 1000) === 0){
print("Total person....: " + total_adress)
print("Iniciando bulk..: " + Date())
bulk.execute({ w: 0 })
bulk = db.company.initializeUnorderedBulkOp()
print("Fim bulk........: " + Date())
print("#############################################################################")
}
}); bulk.execute(); print(total);
Agora vem o problema, quando executo este comando no Mongo o mesmo não dá erro. Já me certifiquei que ele cai dentro do foreach e e busca o meu dado no campo corretamente, o problema é só o update que não funciona.
Obrigado!