How To Join and Print Two MongoDB Collections Using PHP - mongodb

These are two collections
lb_order_details
lb_orders
Can anyone suggest to me how to join -- like MySQL join -- and print these two MongoDB data collections using PHP?

There is no such thing as join in mongodb but you can create relation by keeping id field common in every collection for specific user. You have to manage your database wisely to overcome join issue.

There are no joins in MongoDB. You need to retrieve data with two distinct MongoDB queries and deal with data on PHP side.

Related

How can i delete records that are common in two collections in mongo db

I have two collections and I want to delete common records from both of these two collections shall I join these two collections first?
I have tried to join two collections in MongoDB but the still I cant able to delete the records that are common please suggest a way..
Collection #1 data
<code>
db.academics_collection.insertMany([{Registration_number:04072013002,Degree_enrolled:"BS Computer Science",Enrollment_year:2019,favorite_course:"WAD"},{Registration_number:04072013001,Degree_enrolled:"BS Computer Science",Enrollment_year:2018,favorite_course:"WAF"},{Registration_number:04072013003,Degree_enrolled:"BS Computer Science",Enrollment_year:2010,favorite_course:"CCN"},{Registration_number:04072013004,Degree_enrolled:"BS Computer Science",Enrollment_year:2020,favorite_course:"DSA"},{Registration_number:04072013005,Degree_enrolled:"BS Computer Science",Enrollment_year:2020,favorite_course:"OOP"}])
collection#2 data
db.personal_collection.insertMany([{Registration_number:04013456789,Name:"Ahmer",City_Name:"Islamabad"},{Registration_number:04013456781,Name:"Humera",City_Name:"Kolkata"},{Registration_number:0121234342, Name:"Waheed",City_Name:"Lahore"},{Registration_Number:09072013002,Name:"Zaheer Ahmad",City_Name: "Shakargarh"}])
#join query
db.personal_collection.aggregate([{
$lookup:{
from:"academics_collection",
localField:"Registration_Number",
foreignField:"Enrollment_year",
as:"deleteable_records"
}
}])
I want to delete record that are common in both collections i.e registration number

is there any option in cloud Firestore to use queries like inner join or join?

I have some complicated queries, but don't know how to code for fetching records based on my query, which has relation with one collection to other collection

How to get result from 2 documents in Mongodb

As we already known as we don't have table joins in Mongodb but if we want to get result from 2 different documents than how we can query to Mongodb? Consider following example.
Document 1 - > department
{Id_:123,name:technical,location:"B Wing"}
{Id_:234,name:account,location:"main Wing"}
{Id_:547,name:HR,location"C Wing"}
Document 2 - > employee
{Id_:a101,name:Peter,dept_id:234,DOB:2010-01-01}
{Id_:a102,name:Liomo,dept_id:547,DOB:1950-01-01}
{Id_:a103,name:Juno,dept_id:123,DOB:1990-01-01}
{Id_:a104,name:ole,dept_id:554,DOB:2011-01-01}
So how can we get all fields like (EmployeeName, DepatmentName, DOB) in one result, I am not getting any way please help me out
Thanks in advance
There is no way. Mongodb does not support joins. The NoSQL way is to denormalize your data, meaning you embed a copy in A of the fields from B you need in A.
There is such a thing as database references, but all that does is provide syntactic sugar for combining the result of two queries client side.
By the way, if your data is really relational in nature (like an employee database the way I would probably design it), perhaps a relational database would be a better fit.

How can I effectively join 2 huge collections in MongoDb?

I have two huge (few hundred thousands of records) collections Col1 and Col2, and I need to fetch joined data from both of them. There is a join criteria that lets me dramatically decrease number of records returned to few hundreds, so in SQL language I would run something like
SELECT ... FROM Col1 INNER JOIN Col2 ON Col1.field1 = Col2.field2
and it would run pretty fast as Col1.field1 and Col2.field2 are indexed fields. Is there any direct way or workaround to do the same thing fast in MongoDb with indexes usage, not to scan all the items?
Note: I cannot redesign collections to merge them into one.
MongoDB has no JOIN so there is not a fast equivalent. It is most likely a schema design issue but you said you can't change that. You can't query multiple collections in one query.
You can either do the join client-side in 2 queries or you can do it in non-live style by doing a map-reduce and generating a 3rd collection.
Reference this other question for details on how to do a map-reduce
In order to join in MongoDb 4.2 you can use aggregation and $lookup like this query:
db.collection.aggregate([
{ $lookup: { from: "...", ... } }
])
It is usefull for me
More information: https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/
the join in MongoDB is so expensive. 2 solutions:
Redesign merge them into one
limit, match before you join

make a join like SQL server in MongoDB

For example, we have two collections
users {userId, firstName, lastName}
votes {userId, voteDate}
I need a report of the name of all users which have more than 20 votes a day.
How can I write query to get data from MongoDB?
The easiest way to do this is to cache the number of votes for each user in the user documents. Then you can get the answer with a single query.
If you don't want to do that, the map-reduce the results into a results collection, and query that collection. You can then run incremental map-reduces that only calculate new votes to keep your results up to date: http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-IncrementalMapreduce
You shouldn't really be trying to do joins with Mongo. If you are you've designed your schema in a relational manner.
In this instance I would store the vote as an embedded document on the user.
In some scenarios using embedded documents isn't feasible, and in that situation I would do two database queries and join the results at the client rather than using MapReduce.
I can't provide a fuller answer now, but you should be able to achieve this using MapReduce. The Map step would return the userIds of the users who have more than 20 votes, the reduce step would return the firstName and lastName, I think...have a look here.