findMany is not a function in MongoDB - node.js

I was making a commenting system on my website. I stored all the comment data in MongoDB and now I abviously need to access that data to print out the comments to a user. But, I see that there is no 'findMany' function for the collection object. I just need to get all the code in the collection in an array. findOne finds one document, then findMany should be there right? Is there any other such function in the collection object which can give me all the data in a collection of a database in MongoDB
Any help would be great!

You must use the find().toArray() method instead of findMany({ }) to get all the data there in the collection and to keep that data in an array. This will give you an array with all the data in the collection.

Related

Firestore Subcollections vs mongodb sub documents

What is the difference between the two?
Subcollections store extra data inside a document as a collection and are not part of the document because they must be accessed as a separate call.
Mongodb sub documents store extra documents inside a document property as array and I'm not sure if the whole documents are retrieved in one document call.
Actually, what I'm asking is: How to achieve Subcollections in mongodb?

Firebase Cloudfirestore get elements in a collections

Hello guys i'm trying to query a collection made in firebase cloudfirestore.
How can i create custom queries for objects inside an array.
This is how looks my collection:
"someRestaurantId": [{id:1,desc:"test",creationDate:"12/12/2020"},{id:2,desc:"test",creationDate:"12/12/2020"},{id:3,desc:"test",creationDate:"12/12/2020"}]
I have two problems how i can get only object for id:1 and then querie in a date range using creationDate input?
So far I been getting the full object based in the key: someRestaurantId, and then iterating in my nodejs function, but it could be many of restaurantsIds and many object and i don't want to kill the performance of the query if the object has thousands of results.
Unfortunately, there is now way to retrieve less than one document.

find all documents from mongodb collection with some attribute having value lie in an array

I want to find all documents in a collection where they have property like
Creator and i have an array which contains different values for this creator property and i want to of those documents where these different values in that array are the value of property creator in the document.
I tried by mapping the array and for each array value i tired to find document.
But due to its asynchronous nature i have to promise.all in order get that array of documents.
But when i passed that array to my react app from backend
It is showing error of incorrect react child class.....
You can achieve it using the $in operator from Mongo:
db.customers.find({creator: {$in: ["faizulhassan.a#caratlane.com", "sajid.s#caratlane.com"]}})
Where Customer is your collection name.
Hope this helps..
const answer = collectionName.find({creator: "what you want to find"});
if(!answer){
#do something
} else{
#do something
}

Adding extra value at Model.create with Mongoose

I have a large array that comes in from an API that I'd like to store straight into MongoDB.
Model.create(largeArray) ... // many documents created
The problem is, I have one additional key:value pair that I need to set for all documents in that array. It's a user id, and many documents are created for a given user once per API call. So for a given Model.create call, the user id is the same for every doc in the array.
Without mapping over the array, is there an efficient way of adding a field with a consistent value? Something like Model.create(myLargeArray, {userId: someUserId}) would be ideal, but I know this isn't the case with the Mongoose API.
function addDocsForUser(largeArray, someUserId) {
// each element of largeArray needs to have `userId: someUserId` added to it
return Model.create(largeArray)
}

Mongoose access multiple collection

In mongodb I have one collection named as People.
It contains:
_id:Number,
name:String,
friends:[{friendsId:{type:Schema.Types.ObjectId,ref:'People'},addedTime: Date}]
I want to search a name of friend of a particular user and return the _id of that friend in a single query using mongoose in node.js.
Its impossible to retrieve the data in a single query/populate. Since mongodb is not a relational database. Iterate the data separately & give the result as callback to next query.

Resources