Force new _id generation in embedded MongoDB documents - asp.net-mvc-3

I am working with ASP.NET MVC 3, C# and MongoDB. I have a model with embedded documents, but I would like to auto-generate a new _id for each of my embedded documents.
I can do this in the code and set
Model._id = ObjectId.GenerateNewId();
But I would love it if I didn't have to worry about doing this and let MongoDB auto-generate the new _id for each embedded document.
I do not want to normalize out these embedded documents into a new collection, they make sense here, but I'd like to have a unique ID for them.

The only ObjectId that MongoDB "auto-generates" is the one it uses for the primary key: _id.
When you save a document, MongoDB knows basically nothing about "schema" or "embedded" documents or "arrays of sub-documents". There's no type-checking or schema validation, so there's no way to force the instantiation of the embedded IDs.
Your best bet is to wrap it up in the parent class. If those embedded documents have a specific class tied to them, you can put the GenerateNewId() in that constructor.

Related

Save Related Documents In Mongo Reactive But Not In The Same Collection

I would like to know, how to save related documents in reactive mongo ?. Because I find a code that attempts to do the magic... But when it should save the related document in another collection, it serializes inside the "father" of the relationship let say... I know that in spring data reactive mongo, #DbRef doesnt have support... How could I save the data in a way that, if I query the collection, I see that the attributesare the name of the collection and the generated id instead all of the object attributes ?.
If the pic above is seen, you will see that the attribute "user" is saved as a nested document but not in the corresponding collection. Do I need to hook in another event ?.
I had put a listener onbeforeconvert to scan every time a save operation is to be applied and save the object... How to proceed ?... think I should verify if it has a related doc from another collection and if its nonnull... If the object doesnt have any attr l
Alike, then save it... if not continue the scanning... dunno

How do I map "id" property into "id" field in Mongo without #Field annotation?

I have multiple classes with the id field. I would like to store their instances in MongoDB using spring-data-mongodb. I would like to map id property in these classes to id field in Mongo.
So here is what my classes look like:
public class Entity {
private final String id; // = 42
...
}
And here is what I am expecting to be in Mongo collection:
{
"_id": ObjectId("5fba805dfdaaa760974d45de"),
"id": "42"
}
By default, spring-mongodb maps id property to _id field in Mongo. I know that the simplest way to avoid this is to put #org.springframework.data.mongodb.core.mapping.Field("id") annotation on id property in a Java class. But I prefer not to use this annotation since I would like to keep my model independent of Mongo, or Spring, or whatever. Which options are possible here?
Here is what I have tried or checked:
Registering custom AbstractMongoEventListener in order to modify Mongo documents just before they are written to Mongo, or just after they have been read from Mongo. It does not work for me since custom listener is called only during get and insert operations, but not during the update or upsert operations (see discussion here for details).
Providing custom FieldNamingStrategy — it does not work since in the spring-data-mongodb code they use strategy only if field name is not id or _id.
Providing custom converter for each of my classes. I believe it should work. But this approach seems to be too complicated since I have many classes with many properties in each of them and I'm not sure I would like to have many converters with boilerplate code inside.
Any help would be appreciated.
Don't you think that id with _id is already confusing?
If you have a strong valid reason to have an id (different than DB one); give it a name .e.g: version_id, old_id, other_services_id ... some meaningful name.
And yes using #Field in your case is the simplest way

How should I design index in ElasticSearch for schema less documents with user defined fields

Currently we use Mysql as database for our multi-tenant web application, in order to improve our search we decided to move to ElasticSearch. We have an Entity in Mysql with some base fields and every tenant can define his own custom fields(can be of any data type) for that entity. What are the best practices for designing index in elastic search for above problems?
Will dynamic mapping work fine in the above case.
Elasticsearch is great tool to define custom fields, you simply can index data without define nothing.
In some cases you have to define fields mapping, for example in date field or Geo point field, if you don't mapping this field, elastic will not treat this field as you wish.
So if your custom fields is not date or Geo point you can allow tenant define custom fields.

What's the different between index and update document in elasticsearch?

As we know when we update an existed document the Elasticsearch engine will reindex the document and mark the previous document deleted. But for the restful API, it's same. So I guess the ElasticSearch will analysis the document whether exist by the unique document ID and then update or index.
So my question is, we don't need to care the index or update functionality, because both restful API and Java Client are PUT the same endpoint, Am I right?
The most difference for PUT and POST document in Elasticsearch:
POST will create a new document with a new unique ID.
PUT will update the current document without change ID.
so if your ID is important to you like for some context, you should use PUT to update a document to keep this ID.

Primary Keys and CouchDB

CouchDB's versioning is an absolute boon to the application I'm writing, but each of the objects I want to represent in the database has it's own unique identifier (let's call it my_id), so I don't really need the _id field.
Is there a way for me to tell CouchDB that I want to make my field the primary hey (not _id)?
I'm using ruby's couchrest_model, so I know I can do Model.find_by_my_id(params[:my_id]) if I've put view_by :my_id in my class, but this feels like I'm storing an _id for no purpose. Should I care?
would it not be possible to, when you create the document, provide your own id instead of the default one couchb assigns? I don't know if ruby's couchrest can do it, but it's available in the CouchDB API
See here: http://wiki.apache.org/couchdb/HTTP_Document_API#PUT
The document ID is passed into the url.

Resources