Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

366
Views
$unset matriz 2D MongoDB (C#)

Buenos días,

Tengo un problema con MongoDb, estoy tratando de desactivar un índice de "citas", en mi Json puede ver 1 campo de citas con 2 citas: "Saisir ou Selection une citations" y "XD", me gustaría unset XD, ¿cómo puedo lograr esto?

 { "_id" : ObjectId("604ca13de0059b65e4e67c01"), "username" : "LOL", "pass" : "LOL", "citations" : [ [ { "body" : "Saisir ou Selection une citations", "author" : "author", "oeuvre" : "oeuvre", "annee" : NumberInt(1) } ], [ { "body" : "XD", "author" : "XD", "oeuvre" : "XD", "annee" : NumberInt(3) } ] ]

}

He probado muchas cosas:

 db.getCollection("Users").update( {}, {$unset : {"citations.$.body" : "XD"}} )

y probé muchas otras variantes pero no encontré la solución correcta.

PD:

Estoy trabajando en una aplicación C# Wpf, sería perfecto si pudiera ayudarme en este idioma, pero de lo contrario, no se preocupe, no es muy diferente.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

En la función de actualización, debe dar la primera condición. Entonces es posible que deba usar "$ pull" para eliminar valores de la matriz.

Debería ser algo como esto:

 db.getCollection("Users").update( {citations.body: "XD"}, {$pull : {"citations.$.body" : "XD"}} )
over 4 years ago · Santiago Trujillo Report

0

tendrá que usar los operadores ElemMatch y PullFilter en el controlador C# para lograr su objetivo de la siguiente manera:

 var filter = Builders<User>.Filter.ElemMatch( u => u.Citations, c => c.Body == "XD"); var update = Builders<User>.Update.PullFilter( u => u.Citations, c => c.Body == "XD"); await userCollection.UpdateManyAsync(filter, update);

aquí hay otra forma menos detallada de hacer lo mismo usando la biblioteca MongoDB.Entities (que escribí):

 using MongoDB.Entities; using System.Linq; using System.Threading.Tasks; namespace TestApplication { public class User : Entity { public Citation[] Citations { get; set; } } public class Citation { public string Body { get; set; } } public static class Program { private static async Task Main() { await DB.InitAsync("test"); await new[] { new User { Citations = new[] { new Citation { Body = "Saisir ou Selection une citations"}, new Citation { Body = "XD"}, } }, new User { Citations = new[] { new Citation { Body = "Saisir ou Selection une citations"}, new Citation { Body = "XD"}, } } }.SaveAsync(); await DB.Update<User>() .Match(u => u.Citations.Any(c => c.Body == "XD")) .Modify(f => f.PullFilter(u => u.Citations, c => c.Body == "XD")) .ExecuteAsync(); } } }

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!