Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

737
Visualizações
How to update Reference Array in mongoose

I have a Group Collection, which is having a Reference array of Members. Two Objects are inter-connected like follow. When I am adding new members to the group the members field of Group object needed to be updated. How can I do this with a mongoose update operator.

var MemberSchema = new Schema({
    name:{
        type:String,
        default:null
    },
    user_id:{
        type : Schema.ObjectId,
        ref : 'User',
        default : null
    }
});

var GroupSchema = new Schema({
    name:{
        type:String,
        default:null
    },
    description:{
        type:String,
        default:null
    },
    members:[MemberSchema],

},{collection:"groups"});

Thank You in advance.

Update

I added a sample document of group.

{
    "_id" : ObjectId("586a2e694467c41218b302c3"),
    "members" : [ 
        {
            "_id" : ObjectId("586a2e694467c41218b302c6"),
            "user_id" : ObjectId("58171d75e72bf516f92dcd4e"),
            "name" : "Lakmal Kapukotuwa"
        }, 
        {
            "_id" : ObjectId("586a2e694467c41218b302c5"),
            "user_id" : ObjectId("5821807516325e127f59438e"),
            "name" : "Prasad Perera"
        }, 
        {
            "_id" : ObjectId("586a2e694467c41218b302c4"),
            "user_id" : ObjectId("586263515356e908de6c899a"),
            "name" : "Sadun Prasad"
        }
    ],
    "description" : "Des 1",
    "name" : "My group",
    "__v" : 0
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you are sending the new members as a list of objects with the following structure e.g.

membersListToAdd = [ 
    {
        "user_id": "58171d75e72bf516f92dcd4e",
        "name": "foo"
    }, 
    {
        "user_id": "5821807516325e127f59438e",
        "name": "bar"
    }
]

then use $push with $each modifier in an update as follows:

var query = { name: 'My Group' },
    options = {},
    callback = function (err, result) { console.log(result); };
Group.update(query, { $push: { members: { $each: membersListToAdd } } }, options, callback)
over 4 years ago · Santiago Trujillo Relatório

0

You are doing this wrong,

no need to have links in both collections and no need to nest models

try this instead

var Group = mongoose.model("Group", new Schema({
    name: {
        type:String
    },
    description: {
        type:String
    },
}));

Group.virtual("users", {
    ref: "User",
    localField: "_id",
    foreignField: "groups"
});

var User =  mongoose.model("User", new Schema({
    name:{
        type:String
    },
    groups: [{
        type : Schema.ObjectId,
        ref : 'Group'
    }]
}));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda