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

662
Views
Actualice un documento por _id (nombre de campo BSON no válido _id)

Estoy tratando de actualizar un documento usando el método updateOne :

 UpdateResult r = coll.updateOne( eq("_id", id), this.getMapper().mapToMongoDocumentEntry(entity) );

Sin embargo, recibo una excepción que me dice:

Nombre de campo BSON no válido _id

mapToMongoDocumentEntity devuelve un Document como:

 Document{ _id=588b0d7108980f004323ca73, username=user, password=.---, cname=----, sname=, mail=mail, creation=Fri Jan 27 09:05:52 UTC 2017, validation=null }

mapToMongoDocumentEntry código:

 public Document mapToMongoDocumentEntry(User entity) { Document result = new Document(); if (entity.getId() != null) result.put(UserEntityMongoDocumentMapper.FIELD_ID, new ObjectId(entity.getId())); result.put(UserEntityMongoDocumentMapper.FIELD_USER, entity.getUser()); result.put(UserEntityMongoDocumentMapper.FIELD_PASSWORD, entity.getPasswd()); result.put(UserEntityMongoDocumentMapper.FIELD_COMMONNAME, entity.getCname()); result.put(UserEntityMongoDocumentMapper.FIELD_SURNAME, entity.getSname()); result.put(UserEntityMongoDocumentMapper.FIELD_MAIL, entity.getMail()); result.put(UserEntityMongoDocumentMapper.FIELD_CREATION, entity.getCreation()); result.put(UserEntityMongoDocumentMapper.FIELD_VALIDATION, entity.getValidation()); return result; }

¿Algunas ideas?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

/** * Replace a document in the collection according to the specified arguments. * * @param filter the query filter to apply the the replace operation * @param replacement the replacement document * @return the result of the replace one operation * @throws com.mongodb.MongoWriteException if the write failed due some other failure specific to the replace command * @throws com.mongodb.MongoWriteConcernException if the write failed due being unable to fulfil the write concern * @throws com.mongodb.MongoException if the write failed due some other failure * @mongodb.driver.manual tutorial/modify-documents/#replace-the-document Replace */ UpdateResult replaceOne(Bson filter, TDocument replacement);

debería funcionar bien para usted que el

 /** * Update a single document in the collection according to the specified arguments. * * @param filter a document describing the query filter, which may not be null. * @param update a document describing the update, which may not be null. The update to apply must include only update operators. * @return the result of the update one operation * @throws com.mongodb.MongoWriteException if the write failed due some other failure specific to the update command * @throws com.mongodb.MongoWriteConcernException if the write failed due being unable to fulfil the write concern * @throws com.mongodb.MongoException if the write failed due some other failure * @mongodb.driver.manual tutorial/modify-documents/ Updates * @mongodb.driver.manual reference/operator/update/ Update Operators */ UpdateResult updateOne(Bson filter, Bson update);

Las razones por las que compartí la documentación son para resaltar dos cláusulas importantes:

  1. updateOne readds: la actualización que se aplicará debe incluir solo operadores de actualización y no es una buena idea actualizar el _id de un documento existente, sino reemplazar el documento con uno si lo está generando como en su método mapToMongoDocumentEntry .
  2. Dado que mapToMongoDocumentEntry devuelve el documento completo y no solo atribuye todo el reemplazo del documento, es lo que realmente está buscando en lugar de actualizar los campos del mismo.

Además, tenga en cuenta que puede usar los dos métodos anteriores sobrecargados con un parámetro adicional UpdateOptions que puede proporcionar soporte para la inserción de documentos, upsert bypass etc.

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!