Spring Data MongoDB return object converter - mongodb

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.

Related

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

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.

mongo query to get the object fields dynamically

I have following document which has a filed4 as a object. Inside that object the values are in the key value pair. The keys as well as the values are dynamic i.e what ever the user enters will be saved.
Keys will also be provided by the user as well as the values.
collection{
field1:value1,
field2:value2,
field3:value3,
field4:{
k1:v1,
k2:v2,
k3:v3,
k4:v4,
k5:v5
},
field5:value5
}
I need help in creating a mongo query which can extract this objects fields dynamically without having to specify them in the query.
field1:value1,
field2:value2,
field3:value3,
k1:v1,
k2:v2,
k3:v3,
k4:v4,
k5:v5
field5:value5

Restrict Mongoose return data after fields

Is there any way to restrict my returning data after calling the method data.save() which currently returning me all the data in the collection.
NOTE: Am using mongoose for Mapping

Get a document from couchbase lite database swift

I want to retrieve a document from database to update it, so I need the document ID, how to get this ID if I didn't create the document with a custom ID (I've used database.createDocument() instead of database.documentWithID("docId"))
Thanks
If you haven't created the document with a custom Id then you can use
some other uniquely identifying property (or properties) of the document to query for the specific document.
Instead of querying and iterating over entire list of documents to find the match, you can create a view with the specified property/properties as index. You can then query for that view with startKey / endKey set to the desired property value. That will return the document of interest.
Alternatively,if it's an option, create the document with a custom ID.

Ways to refer to a database, collection, and document in MongoDB?

use <databasename> will set a variable db to be the database
specified by <databasename>, so the database can be referred to
the variable db.
I wonder if a collection or document can also be
referred to by a variable, and if yes, how?
Every object in a MongoDB server has an identifier _id. If I am correct, a database, a collection and a document are objects.
How are the identifier of an object used in practice?
Both a database and a collection have a name. So we can refer to a
collection via its name e.g. mydb.mycollection.
Does a document also have a name?
Thanks.
A collection consists of several documents so a document as such do not have any name. _id distinguishes the documents. To fetch any particular document, you can filter it on the basis of _id or the data stored in it.
Referencing the db and then the collection in selected db will give you the required document.