MongoDB Embedded Objects have no ID (null value)

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

I have a question regarding MongoDB with Spring Data. I have these domain classes:

@Document public class Deal  {     @Id     private ObjectId _id;     private Location location;     private User user;     private String description;     private String title;     private String price;     private boolean approved;     private Date expirationDate;     private Date publishedDate; }  @Document public class Location {     @Id     private ObjectId _id;     private Double latitude;     private Double longitude;     private String country;     private String street;     private String zip; }  @Document public class User {     @Id     private ObjectId _id;     private String email;     private String password;     private String profile_image_url;     private Collection deals = new ArrayList(); }

The result after saving a User with one deal:

{ "_id" : ObjectId( "4fed0591d17011868cf9c982" ),   "_class" : "User",   "email" : "milo@gmail.com",   "password" : "mimi",   "deals" : [      { "_id" : null,       "location" : { "_id" : null,         "latitude" : 2.22,         "longitude" : 3.23445,         "country" : "Denmark",         "street" : "Denmark road 77",         "zip" : "2933" },       "description" : "The new Nexus 7 Tablet. A 7 inch tablet from Google.",       "title" : "Nexus 7",       "price" : "1300",       "approved" : false,       "expirationDate" : Date( 1343512800000 ),       "publishedDate" : Date( 1340933521374 ) } ] }

As you can see from the result, Deal and Location ID is set to NULL.

回答1:

MongoDB CRUD operations (insert, update, find, remove) all operate on top-level documents exclusively -- although of course you can filter by fields in embedded documents. Embedded documents are always returned within the parent document.

The _id field is a required field of the parent document, and is typically not necessary or present in embedded documents. If you require a unique identifier, you can certainly create them, and you may use the _id field to store them if that is convenient for your code or your mental model; more typically, they are named after what they represent (e.g. "username", "otherSystemKey", etc). Neither MongoDB itself, nor any of the drivers will automatically populate an _id field except on the top-level document.

Specifically in Java, if you wish to generate ObjectId values for the _id field in embedded documents, you can do so with:

someEmbeddedDoc._id = new ObjectId();


回答2:

An _id is not set on subdocuments by default only on root docuemnts.

You will need to define a _id for your subdocuments on insert and update.



回答3:

In the context of a REST architecture it makes all the sense that nested docs have their own Ids.

  1. Persistence implementation should be independent from resource representation. As an API consumer I don't care if you are using mongo or mysql. If you a nest docs without id in mongo try to imagine how to change the persistence layer to a relational data base. Now do the same exercise having thought beforehand with an implementation independent approach. Modeling nested docs in a relational db, root and nested docs will be different entities/tables each with their own ids. The root could have a one to many relationship with the nested doc.
  2. I may need to access a nested doc directly instead of sequentially.I may not need absolute unique ids, like those issued by mongo, but I'll still need some local unique identifier. This is, unique within the root doc.

Having argued my point about the need of ids in nested docs @dcrosta has already given a correct answer on how to populate the _id field in mongo.

Hope this helps.



回答4:

Mongo does not create or need _ids on embedded documents. You can add an _id field if you want - I have done it.

@Document public class Location {     @Id     private ObjectId _id;      public Location() {         this._id = ObjectId.get();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!