How to create document using mongoose in express-node app


Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.

MongoDB

MongoDB is a NoSQL database which store data as JSON document and collections. In JavaScript applications we can utilize the mongoose driver to perform Mongo operations. First up all we need to install mongoose to your project and then import the module.

npm install mongoose

Saving the document

mongoose uses the modal class to perform REST operations. If you are not familiar have a look at the mongoose modal post given below

Tag.create({
tagName: tagNa}, function(error, stag) {
if (error) return handleError(error)
 console.log(new tag saved : ${stag.tagName});
})

The Tag is the Modal and the create method is used to save the document to database and it also use a callback function to return the result.

We can also use an asynchronous method find documents in Mongoose as follows

const tag=new Tag({tagName:'SQL'})
await tag.save()

That’s it. Following mongoose posts deserve your attention

Filter Firebase collection in Future builder in Flutter


Cloud Firebase NoSQL can blazingly fast live data to your Flutter app. We can rely on Stream builder or Future builder to get a list of collections as we learn in the below posts.

Filtering future

How about filtering some collection based on fields ? We can filter collection using where clause then call the documents/get method to get the document snapshot as follows

 FutureBuilder(
           future: FirebaseFirestore.instance.collection('carts').where('Finished',isEqualTo: true).get(),
           builder: (context, snapshot) {
             return snapshot.hasData

Here the future return only those documents in the collection from carts whose Finished field is equal to true. Here isEqualTo: accept the document type value, it can be a string, bool or a number, can be any Firestore document type.

How to use FutureBuilder to create a live listview from Cloud Firebase database in Flutter


We have already learn how to build a Firebase Firestore collection listview using StreamBuilder and a Listview builder. Dart has capable of async operations which promise error free data integration for you app. Flutter also support Future which is Flutter API for pure async operations.

Future builder takes a Future which is the data or function that return promising data or error and Future Builder function which extract those data . Here is sample code

body: Container(
        child: FutureBuilder(
          future: FirebaseFirestore.instance.collection('karts').get(),
          builder: (context, snapshot) {
            return snapshot.hasData
                ?   ListView.builder(
              itemExtent: 80,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) {
            return ListView.builder(
              itemExtent: 80,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) {
                return ListTile(title: Text(snapshot.data.documents[index]['name']);
                }
                );}))

The future builder block the other section until it get access to the collection ‘karts’ as a result the UID automatically get updated.

Filtering data in a Future

We can also query data in future using the where , which will help use to filter the data in place.

future: FirebaseFirestore.instance.collection('products').where('mfr',isEqualTo: 'TATA').get(),

The above line in the future of a FutureBuilder fetch only those document ( row ) which has ‘mfr’ field and equal to ‘TATA’. The exact method can also applied to a StreamBuilder too.

How to use StreamBuilder to create a live listview from Cloud Firebase database in Flutter


Firebase is NoSQL database which offer live data for applications that run on mobile devices. It is work with Google cloud services. You can build simple data-application by creating a new Firestore database and follow those steps to integrate the service with your Android/iOS Flutter app.

This tutorial demonstrate how you can use a Stream builder to pull data , live data from the Cloud Firestore database to your device. The live data is made possible with Snapshot subscription.

body: Container(
          child:child: StreamBuilder(
            stream: FirebaseFirestore.instance.collection('karts').snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) return Text('loading ...');
              return ListView.builder(
                itemExtent: 80,
                itemCount: snapshot.data.documents.length,
                itemBuilder: (context, index) {
                  return ListTile(title: Text(snapshot.data.documents[index]['name']);
                  }
                 );}))

snapshot() subscription enables Flutter UI get notified whenever the collection get updated (add/delete/update) , before we filling the data to the Listview , made quick validation for the data using expression ( ? ), this is because sometimes data get blocked and will cause for some error.