Which is the best way to do database search on mean stack - node.js

I am using mongodb to store items for an auction site
I want to enable fuzzy searching.
Should I query for 1000 results with no parameters then use a js library like fuse.js
Or should I rely on mongodb $regex alone to do the query?

mongodb isn't a great choice for a problem like this. There are lots of great text search utilities available, the most prominent these days being elasticsearch. You'd continue to store your data in mongodb, but you'd keep an elasticsearch instance synced to the mongodb database and perform your searches against elasticsearch. Mongoosastic is a good way to write to both concurrently or Transporter can be used shift the synchronization away from your database persistence flow.
Mongoosastic example:
https://blog.cloudboost.io/sync-mongo-with-elastic-and-save-months-of-development-time-and-cost-d281e0ca8fe4
Some other ways to sync including Transporter: https://code.likeagirl.io/5-different-ways-to-synchronize-data-from-mongodb-to-elasticsearch-d8456b83d44f

Related

Call function when document is added to database?

I am working with NodeJS and Cloudant (alternatively the DashDB Warehouse if that works better). I wonder if it is possible to have a function in NodeJS that gets called each time a document has been added to the database? I have checked out indexed views but can't really understand how to do it. Does anyone have any good tips regarding this or what documentation to look at?
You can listen to DB _changes in Cloudant (https://console.bluemix.net/docs/services/Cloudant/api/database.html) in continous mode.
Every document change in the Cloudant DB (create,update,delete) will be notified through this channel.
There are different nodejs libraries you can use with this purpose. This is
one example: https://www.npmjs.com/package/cloudant-follow

bookshelf without knex - execute query

I have a functioning node.js application with logs data to a mysql database. (without using knex.js)
Now, I want to add functionality to query into my database tables. My question is do I now need knex.js? Is it possible to execute queries without knex?
I could not clearly find examples of this.
From the main Bookshelf page:
Bookshelf is a JavaScript ORM for Node.js, built on the Knex SQL query builder. Featuring both promise based and traditional callback interfaces, it follows the Model & Collection patterns seen in Backbone.js, providing transaction support, eager/nested-eager relation loading, polymorphic associations, and support for one-to-one, one-to-many, and many-to-many relations.
Bookshelf requires a knex connection to operate. You cannot (nor should you) use Bookshelf without Knex.

How to get all document from couchdb start with some word. is it possible in light couch?

I am using pouchdb on client side and couchdb on server side. and both are in sync.
I am accessing couchdb from java using client-api lightpouch.
I am storing transaction data, each transaction is stored as document by prefixed _id like
Transaction_1,
Transaction_2
..
..
so on
Now i want to access all the documents where the _id field starts with Transaction on the server.
This is possible in pouchdb and i am able to achieve that.
But i am wondering how can i achieve the same at server side, in java using lightcouch.
Or is there any Java client-API available that provides this kind of functionality. ??
To find all documents whose _ids match a certain prefix, you only need to do:
/_all_docs?startkey="foo"&endkey="foo\uffff"
(For the prefix "foo".)
I wrote up a bit about why this works here.
LightCouch aims at providing a simple API
for communicating with CouchDB databases.
What you need is a CouchDB view server-side which you can request with LightCouch.

Saving chat transcripts in nosql databases

I'm building a chat server app using node.js.
I need to save chat transcripts on the database and would like
to play with nosql database like mongodb. If i was in relational db world, i would
create users, chat_sessions and chat_messages tables and, for every new message, i'd
append new record in chat_messages table.
Which is the best approach in nosql?
Do i have to create a chat_session document and, inside of it, create
a chat_messages structure that is updated for every new message or
is there a better way to do it for nosql/mongodb?
You would use a similar approach and insert each new message as a separate document into the collection (possibly one for each room or channel).
Documents in MongoDB have a 16mb limit so storing the entire history in one document, which grows in an unbounded fashion, would be a bad choice.
You can de-normalize usernames and store them on the messages themselves to avoid querying two tables (this would likely be a join in a relational database).
It may make sense to split the data into multiple databases rather than collections (per room, channel, whatever) because currently mongodb has a database level write lock. This would allow you to achieve greater write concurrency with mongodb and node.js.

Retrieving data from couchDB

I am new to couchDB but have a good experience working with relational databases. Can anyone tell how to connect to couchDB database and retrieve the data stored in it. I am giving an example in relational database and i need help regarding how to do similar task in couchDB.In mysql we use a connector to get connected to the database and the for example we give "select username from tablename where password="abc" ".
CouchDB talks HTTP and JSON, then you can use any HTTP client and JSON parser/generator. You can find a nice introduction in The Definitive Guide.
Try this URL: http://localhost:5984/_utils/, it will open FUTON editor.
CouchDB is a NOSQL database. So it works using HTTP requests (url based). Data that is stored in couchDB is in the form of JSON documents, so there is no concept of tables. In short, database in SQL represent database in couchDB and the rows in a table of SQL represent Documents in couchDB.
Coming back to your question, to retrieve data from couchDB, there is a concept called views which uses Map and Reduce functions (which are JavaScript functions). Using these views couchDB indexes your search function spanning through the complete database (includes all documents), so you need to write a Map function specifying the condition to be used to search. Here's an example -
function(doc) {
if (doc.password) {
emit(doc.username, doc);
}
}
The above example is a simple Map function. Search for the documents of the database where there is a password and return the usernames from all the documents in the database. Password input value (in this case "abc") should be specified in the query string that you will be sending out to couchDB URL. Now, you might ask where is the database specified to search for? I said that we have to create views in order to search. These views are stored in that particular database where you want to search. So, if you want to search a database with name "User_Credentials", then create a view in the "User_Credentials" with the above Map function. More details on how it can be done can be found here: CouchDB Guide to Views

Resources