How to fetch specific field from mongo collection using morphia, if that field is removed from mapped POJO class? - mongodb

Lets say class "A" as follows:
Class A{
String fieldOne;
String fieldTwo;}
and collection "collactionA" as follows:
{
"_id" : ObjectId("5f6b2f9f97864348632356d4"),
"fieldOne" : "one",
"fieldTwo" : "two" }
Now i delete field "fieldOne" from class A but as it is present in collection, and want to fetch value of this removed field "fieldOne".
dataStore.createQuery(A.class)
using above method only gives only field which is present in new definition of Class A(remaining "fieldTwo")
How can i fetch that removed field("fieldOne")?

You'll have to query the collection directly using the driver so that you can get the documents back in a Document rather than an A. Morphia will map all the fields in the document to any mapped fields in your entity but it will silently skip over any unmapped keys in the fetched documents. So you'll need to get the "raw" Documents back from the database to fetch those values.
I haven't tried this, personally, but it has a decent chance of working:
var query = datastore.find("collectionA", Document.class);
That should fetch the documents from the collection but stop at mapping them only to Document instead of trying to hydrate entities to hold the data.

Related

Presto & MongoDB - Schema creation and updates

I have the Presto set-up done locally and am able to query data from MongoDB collections. At the start, I created presto_schema collection into MongoDB to let Presto understand the Collection details in order to query and I had one Collection entry added into my presto_schema. However, I noticed later that any new Collection into MongoDB, which was not added into presto_schema is still accessible from Presto and upon the first query it is observed that the new collection details are automatically amended into the presto_schema collection with the relevant new collection schema details.
But for the collections with nested schema, it is missing to automatically add all the nested fields and it only adds what it identifies from the initial query.
For example, consider below is my Collection (new_collection), which it got created newly with content as below:
{
"_id" : "13ec5e2a-ef04-4d05-b971-ef8e65638f83",
"name" : "npt",
"client" : "npt_client",
"attributes" : {
"level" : 697,
"country" : "SC",
"doy" : 2022
}
}
And say if my first query from Presto is as below:
presto:mydb> select count(*) from new_collection where attributes.level > 200;
The presto_schema gets automatically added with a new entry for this new collection, however, it adds all the non-nested fields info and the nested fields too that are available from the initial query, but fails to add the other nested fields. So any queries on the other nested fields, Presto does not recognize them. I could go ahead and amend the presto_schema with all the missing nested fields, but wondering if there is any other automated way. So that, we don't need to keep amending it manually on any new field addition into the collection (considering a scenario where we have a complete dynamic fields, which would be added into the Collection's nested object).
I would recommend upgrading to Trino (formerly PrestoSQL) because the MongoDB connector (version >= 360) supports mapping fields to JSON type. This type mapping is unavailable in prestodb.
https://trino.io/download.html

Avoid duplicate document entry in mongodb spring boot

I hava collection Cars in mongodb.Each documents has fields like brand,model,color,horsepower,torque etc.I want each document to be unique and if entered again it shouldn't get added to collection.
If two fields are unique we can use function like this
public interface CarRepo extends MongoRepository<Car,Long> {
Optional<Car> findByBrandAndModel(String brand,String model);
}
Any suggestions if all the fields in document are unique ? I want to check before inserting document into collection.

How can I achieve sorting on spring data mongodb on multiple collections?

I need to sort the data based on the field present in Collection B's Document, by querying on Collection A's document, and A and B can be related by ID.
Query query = new Query().with(pageable);
List<A> objects = mongoTemplate.find(query, A.class);
With Pageable object, I can easily achieve sorting and pagination on fields present in Collection A and I want to achieve the same thing for field present in Collection B.

Saving mongodb Id in any key as string vs saving as ObjectId

I wanted to know if there is any performance gain or loss in case I save any existing mongoId as string in any other document key. For example I have two collection ans i am saving one collection document Id i.e is _id to another document key as string . I am not going to use it as ref but it is for Viewing purpose and later i can use it to fetch the information from another collection. Example
One doc
{_id : ObjectId() , Name : "Test"} // This is one document
Second doc in another collection
{_id : ObjectId , Detail : { AID : ObjectIdASString } } // ObjectIdAsString is Id from first document
I would store it as ObjectId, ObjectIds require less space on disk. Plus, it would be easier and more efficient to sort your collection based on ObjectId rather than strings. However, nothing scary about it, you can easily convert ObjectId to string and vice versa using toString() and valueOf() methods respectively.

Spring Data MongoDB return object converter

I have a large documents with 50 different fields in it. Only few fields are required when I save this document. When I fetch that document though Spring Data MongoDB is it possible to eliminate null value fields from the object?
During the save Spring Data MongoDB does the same thing and only save those values which are not null.
Our Query class has a fields() method which returns a Field object that allows defining which fields to be read from the document (Javadoc). Thus when executing the query object against a collection you get partial document back and thus a partially filled domain object.