Restrict Mongoose return data after fields - mongodb

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

Related

Check multiple Document exists id Firestore Collection

I want check within a list of 10 document IDs how many exists in a collection. One way to do it would store the document ID inside the document and use in operator.
const result = await this.afs.collection(path).where('id', 'in', ['doc1', 'doc2']).get();
Is there a way we can avoid storing the document id inside document.
For this particular query, you can use FieldPath.documentId(). It returns a token that you can use in the field path of the query:
this.afs.collection(path).where(FieldPath.documentId(), 'in', ['doc1', 'doc2'])
I've linked you to the plain JavaScript documentation. If you're using Angular, it might have a slightly different way of getting this token value, but it will be in a class called FieldPath.

How to insert data into mongodb proper method

What is the proper way to insert data into mongodb? I am able to see other collection by nodejs api call but if i use below method data are inserted but i am not able to see the data on my nodejs api call.
So How to resolve this isse?
Here is my data:
db.trade.insert([
{
"p_id":"ot1",
"product_name":"Mixed",
"product_weight":"1kg",
"product_price":550,
"product_image":"rose.jpg"
},
{
"p_id":"ot2",
"product_name":"Mixed",
"product_weight":"1kg",
"product_price":550,
"product_image":"rose.jpg"
},
{
"p_id":"ot3",
"product_name":"Mixed",
"product_weight":"1kg",
"product_price":550,
"product_image":"rose.jpg"
}
]);
the insert() function takes a single document, while insertMany() takes an array of documents.
In your code, you're attempting to pass an array (multiple documents) into insert() (which expects a single document).
You could either make multiple calls to insert() or a single call to insertMany().

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.

Mongodb, get full object from a reference

Does exist anything permitting us to access a full object from a reference with Mongodb ?
For example, I have a User and a Type collection. A user has a Type, stored with a reference to the Type object.
Is it possible to access the full User object with the Type, without lazyloading it ?
Thanks for all
Yes; if you're happy making use of mongoose, then you can use its populate() function:
Populated paths are no longer set to their original _id , their value is replaced with the mongoose document returned from the database by performing a separate query before returning the results.
http://mongoosejs.com/docs/populate.html
So for your User, when performing a query to derive said user, something like this would set up the Type instance:
User.findOne({ username: 'Fred Bloggs' }).populate('type')
.exec(function (err, user) {
...
MongoDB does not do joins. It's not possible to get the information to embed the full Type object in the User document without more than one operation (I guess you have to "lazyload" it, in your terminology). The Mongoose populate() function just handles doing the extra queries and replacing the id with the document for you - it does multiple queries just like any other client doing the same operation would have to. You should think carefully about what types of queries you are doing to determine if it might be a good idea to denormalize the Type object into User documents.

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.