Firebase Cloudfirestore get elements in a collections - node.js

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.

Related

findMany is not a function in MongoDB

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.

Changing collection name according to query parameters in Nestjs

Can we get data from different collections of same or multiple mongo databases according to the query parameters in nest js ?
For example if parameter says get data from collection A, then collection A data should be displayed if it says get data from collection B, then collection B data should be displayed.
Can we do it in same controller or we need to make multiple controllers ?
I got it, I just have to make two models from different collections and use the desired model according to query parameter by using simple if then else.

Multiple Document Upsert in MongoEngine

I have been really struggling to get multi-doc upserts to work in Mongoengine for Python. I may not be thinking about the query structure correctly or how the mongoengine documents actually work.
Currently, I have to upsert each document individually and it's awfully inefficient. The process is that i'm receiving messages, generating mongoengine documents, storing them in a list, and then attempting to pass that to a collection, but it doesn't work at all.
code looks like this:
new_docs = []
for data in data_for_docs:
new_docs.append(create_doc(data))
Definitions.objects.update(new_docs, upsert=True, multi=True)
I've read a few posts that show the method to_mongo, but i'm not sure how that could help in this instance.

Can we post an array of objects with knex?

I'm using knex with node and I was thinking of making a field in the migration that would take an array of objects. I know simple stuff like
.string("bla")
.notNullable()
and so on, looking in the documentation can't seem to find it
wanting something like
.array_of_objects //field name
knex.schema.createTable('users', function (table) {
table.increments();
table.string('name');
table.timestamps();
//example
table.array
})
With table.specificType you can create which ever type of database column you like. You need to check from your database, which kind of columns it supports.
It would make easier to answer the question if you could tell what kind of SQL queries you are after. To be able to use knex, you need to know SQL. Knex will never abstract SQL away.
If you are looking for a database library for node.js that abstracts SQL away you might like to checkout sequelize.
Thanks for the post with the specificType. From what I saw PG didn't have an array of objects field, but did have an array of strings field. I ended up taking my array of objects and separating it into two string arrays one named keys and the other values. I stored both of those arrays in the database. Then when I did a get request I took the two arrays joined them together into an array of objects with some javascript tacking. Seems to do the trick.

what's the best way to bind a mongodb doc to a node.js html page

In past with my PHP / Rails - MYSQL apps I've used the unique ID of a table record to keep track of a record in an html file.
So I'd keep track of how to delete a record shown like this (15 being the ID of the record):
Delete this record
So now I'm using MongoDB. I've tried the same method but the objectID ._id attribute seems to be a loooong byte string that I can't use conveniently.
What's the most sensible way of binding a link in the view to a record (for deletion, or other purposes or whatever)?
If the answer is to create a new id that's unique for each document in the collection, then what's the best way to generate those unique id's?
Thank you.
You could use a counter instead of the ObjectID
But this could create a problem when inserting a new document after you deleted a previous one.
See this blog post for more detail info on Sequential unique identifiers with Node.js and MongoDB.
Or you could use the timestamp part of the ObjectID:
objectId.getTimestamp().toString()
See the node objectid docs

Resources