Choose data from mongoDB in browser and display results - node.js

I am new to NodeJS, MongoDB and express.
I am able to insert, delete and update data but I dont really know how to work with it.
I want to choose some data(e.g. from a dropdown form) and then pass that data to some functions and calculate results. How is it possible to show the results to the client in the webpage(e.g. in a table)?
For example: I have different JSON objects stored in my database and now I want to select one by the name. Then do some calculations with it and show the results to the client.
Also: Is it possible to tell the Database to only accept valid JSON data?

Yes you can get specified data from the database and it will work a lot better with mongoose. You can define schema in mongoose and you can focus more on the data instead of how your data look like. And you can do validation to accept the valid data.

Related

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.

How to get #servestamp firestore after add new Document

I add some data to firestore which contain #serverstamp, but after I add data , I need to get #serverstamp for other progress
If you're using server side generated timestamps you'll need to query the created document to read the actual timestamp value. Nonetheless, if you need the timestamp maybe it would be more practical to generate the timestamp client side, so the document read isn't needed.

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.

nunjucks not outputting all data from MongoDB

I am using node.js as a scripting language and I am also using nunjucks as the template engine. I have a weird situation going where some of the data that I retrieve from the MongoDB database is not being printed into document.
As you see hear these are the values that I want to print to the web page
But when you look at the web page, only some of the data has been printed out and the other information is missing.
I console.logged the data to prove that the values are in the database
A weird thing is that if you write the whole object into the code, like so
It will output all the data in one block. That includes the month, the year, and slug property that I am trying to output to the page. Yet, it only does that if I print out the whole object
I found out what was the problem. In my mongoose schema, I did not have the properties listed like slug, month, or date. So when I tried to retrieve data from those properties it did not output them. So If you are having similar problems make sure the properties are declared in your mongoose schema

Use datasource to get backend document

I got a panel which bind to an open document as data source called document1 in an extlib dialog box. On button save, I want to compare all the field values between back-end document and document1. However, all the field values from back-end document are seemed to be updated therefore their field values are the same in document1. The comparison is done before docuemnt1.save().
From my understanding, document1.getDocument() should get the back-end document which all original/current data. document1.getDocument(true) should get all new data. I had try to getDocumentByID and found that all field values are updated in back-end document. I have no idea because document1.save not yet executed.
Why back-end document being updated with new data before save?
Is there any better way to get back-end document which all original/current data?
If you are using the the parameter true with the getDocument method, all changes done to the datasource are temporarly written to the datasoure's datastore.
The XPages engine is "smart enough" to realize that multiple instances of a NotesDocument object are all referencing the same backend document. It will now return the cached data from the datasource's datastore. That is why all objects will now return the updated values instead of the values stored in the backend document (Using of multiple datasources will give you the same result).
To access the data from the backend document, you can use a #DbLookup on a view with all documents sorted by their UNID.
#DbLookup("","AllByUNID", document1.getDocument( true ).getUniversalID(), "FIELD")

Resources