How to connect to a collection in mongodb - mongodb

I want to use the collections as DBs!.. make a connection from nodejs where you can use a collection as a normal database.??
The purpose of this is that I'm going to create two totally different apps, with different collections and documents, but there are data that I need to relate between the two apps. And if the two apps are using the same database, it would be much easier for me.

Well.. MongoDB doesnt do that, what can you do is something like give a prefix name to your collections, "projectA_Users , projectB_Users" and to relate data use the $lookup Aggregate operator to get your relationship. I see something like that on FireBase that you can create a collection inside a document and use find, update, remove on it (here)

Related

How to create search that shows results from multiple firestore collections in Flutter?

I have 2 different Firestore collections namely 'restaurants' and 'dishes'.
I currently have created 2 separate searches where user either searches for a dishes in search1 connected to 'dishes' collection or a dish in search2 connected to 'dishes' collection.
Please suggest on how to have just one search which searches in both collections in the backend and shows results from both 'restaurants' and 'dishes' at one place. Something like a universal search. So if user types 'burger' it should show 'chicken burger dish' as well as 'burger kind restaurant'.
Was able to make the searches work independently but need help with the strategy to combine the search in to one.
Thanks for your help.
Please suggest how to have just one search which searches in both collections in the backend and shows results from both 'restaurants' and 'dishes' in one place.
There is currently no way in which you can perform a search in two different collections at the same time. Why? Because the queries in Firestore are shallow, meaning that they only get documents from the collection that the query is run against. So to solve this, you have to perform two separate queries.
There is a workaround that might help, which would be to add all the necessary data on which you want to perform the search in a single document, most likely into a field of type array. In that way, you can perform a search using the "array contains" operator. Or for small datasets to search using contains.
If the above solution doesn't work for you, then you have to implement a third-party search service, as mentioned in the official documentation.

firebase Deep collectionGroup query in firestore

I have a collection structure in Firestore that looks like:
/teams/**/days/**/milestones/**
/teams/1WCraAZXLSNSoMvkZuSV/days/20210822/milestones/xyzdocid
So, I would have a set of documents in the milestones collection at the end. I'm trying to query milestones as a collectiongroup. How do I setup a collectionGroup index for this?
Is this too deep for the collectionGroup to work? What does the index look like for this? Is it possbile to create this collectionGroup index in the firebase emulator?
To anybody running into this same problem:
In the Emulator, collectionGroups and all index related operations automatically work. You don't need to setup anything special. This wasn't very well documented. I spent quite some time trying to find out how to create an index in the emulator. This also means, that you will need to do quite a bit of testing with a live firebase account, because most things will not automatically work there.
The collectionGroup format is based on the name of the collection only and not your indexes or any kind of paths. Meaning, if you have collections called "milestones" anywhere in your structure, the collectionGroup concept will combine them together. This is very powerful, but be careful about collections / subCollections with the same name but different purpose.

Collection or documents for multiple projects?

I want to manage multiple projects data in mongoDB. Each project contains multiple users from multiple departments with multiple role assigned to them. plus certain task is assigned to each user. Now I am confused about schema, not able to decide which entity should be kept as collection & which one as document ? What is the best efficient way to store ?
should I keep all under single collection as embedded documents or in separate collection ?
Thanks
First of all if you are using mongodb you should know why are you using it. MongoDB is not about normalize stuff. If you are able to create data structure is de-normalize way then and only then go for MongoDB.
I think you should maintain one single document containing all the mentioned things above. But the scenario which you have mentioned above is good for relational database. you need only 3 entities in relational database and your problem is solved.
Still if you want to go for mongodb you can go with one collection only. which contains project details number of users working there and their roles and department.

MongoDB - Aggregating multiple collections into a single collection under different keys

I have a MongoDB database with about 100 collections. The collections are very similar to each in structure, but the data is different enough that I need to keep the entries in the collections separate. So I am trying to figure out how I can aggregate all the individual collections into a single collection under different keys.
For example, my database currently has the collections:
collection_set_A
collection_set_B
collection_set_C
collection_set_D
...
and I would like to have a single collection that looks like this:
collection_set
|
+-collection_set_A
+-collection_set_B
+-collection_set_C
+-collection_set_D
+-...
So that collection_set_A can now be accessed as collection_set['collection_set_A'].
Is this possible? I have seen a lot of references to map/reduce, but those seem to be more for joining data, and not full collections like this. I'm basically wanting to move data, not join it.
Does anyone know if this is possible?
This is not possible, but a work-around would be to use the copyto method db.collection.copyTo() to copy the contents of one collection to another collection
You can find more information here http://docs.mongodb.org/manual/reference/method/db.collection.copyTo/#db.collection.copyTo
A somewhat similar question is answered here
Can a MongoDB collection have inside it another collection?

SQL view in mongodb

I am currently evaluating mongodb for a project I have started but I can't find any information on what the equivalent of an SQL view in mongodb would be. What I need, that an SQL view provides, is to lump together data from different tables (collections) into a single collection.
I want nothing more than to clump some documents together and label them as a single document. Here's an example:
I have the following documents:
cc_address
us_address
billing_address
shipping_address
But in my application, I'd like to see all of my addresses and be able to manage them in a single document.
In other cases, I may just want a couple of fields from collections:
I have the following documents:
fb_contact
twitter_contact
google_contact
reddit_contact
each of these documents have fields that align, like firstname lastname and email, but they also have fields that don't align. I'd like to be able to compile them into a single document that only contains the fields that align.
This can be accomplished by Views in SQL correct? Can I accomplish this kind of functionality in MongoDb?
The question is quite old already. However, since mongodb v3.2 you can use $lookup in order to join data of different collections together as long as the collections are unsharded.
Since mongodb v3.4 you can also create read-only views.
There are no "joins" in MongoDB. As said by JonnyHK, you can either enormalize your data or you use embedded documents or you perform multiple queries
However, you could also use Map-Reduce.
or if you're prepared to use the development branch, you could test the new aggregation framework though maybe it's too much? This new framework will be in the soon-to-be-released 2.2, which is production-ready unlike 2.1.x.
Here's the SQL-Mongo chart also, which may be of some help in your learning.
Update: Based on your re-edit, you don't need Map-Reduce or the Aggregation Framework because you're just querying.
You're essentially doing joins, querying multiple documents and merging the results. The place to do this is within your application on the client-side.
MongoDB queries never span more than a single collection as there is no support for joins. So if you have related data you need available in the results of a query you must either add that related data to the collection you're querying (i.e. denormalize your data), or make a separate query for it from another collection.
I am currently evaluating mongodb for a project I have started but I
can't find any information on what the equivalent of an SQL view in
mongodb would be
In addition to this answer, mongodb now has on-demand materialized views. In a nutshell, this feature allows you to use aggregate and $merge (in 4.2) to create/update a quick view collection that you can query from faster. The strategy is used to update the quick view collection whenever the main collection has a record change. This has the side effect unlike SQL of increasing your data storage size. But the benefits can be huge depending on your querying needs.