How to realize Relational Operation in MongoDB
How to implement relational operation in MongoDB? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Relational query operations supported in MongoDB: greater than ($gt), less than ($lt), greater than or equal to ($gte), less than or equal to ($lte), not equal to ($ne), equal to (key:value,$eq).
Example: define a collection of employee information
Db.emp.drop ()
Db.emp.insert ({"name": "Zhao Yi", "sex": "male", "age": 30, "sal": 1000, "loc": "Beijing"})
Db.emp.insert ({"name": "Qian er", "sex": "female", "age": 22, "sal": 5000, "loc": "Shanghai"})
Db.emp.insert ({"name": "Sun San", "sex": "male", "age": 40, "sal": 2000, "loc": "Shenzhen"})
Db.emp.insert ({"name": "Li Si", "sex": "female", "age": 30, "sal": 7000, "loc": "Beijing"})
Db.emp.insert ({"name": "Friday", "sex": "female", "age": 30, "sal": 6400, "loc": "Beijing"})
Db.emp.insert ({"name": "Wu Liu", "sex": "male", "age": 30, "sal": 2500, "loc": "Chongqing"})
Db.emp.insert ({"name": "Zheng Qi", "sex": "female", "age": 50, "sal": 4700, "loc": "Chengdu"})
Db.emp.insert ({"name": "son of a bitch", "sex": "male", "age": 35, "sal": 8000, "loc": "Beijing"})
> db.emp.find () .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be26")
"name": "Zhao Yi"
"sex": "male"
"age": 30
"sal": 1000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be27")
"name": "Qian er"
"sex": "female"
"age": 22
"sal": 5000
"loc": "Shanghai"
}
{
"_ id": ObjectId ("599108423268c8e84253be28")
"name": "Sun San"
"sex": "male"
"age": 40
"sal": 2000
"loc": "Shenzhen"
}
{
"_ id": ObjectId ("599108423268c8e84253be29")
"name": "Li Si"
"sex": "female"
"age": 30
"sal": 7000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 6400
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2b")
"name": "Wu Liu"
"sex": "male"
"age": 30
"sal": 2500
"loc": "Chongqing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2c")
"name": "Zheng Qi"
"sex": "female"
"age": 50
"sal": 4700
"loc": "Chengdu"
}
{
"_ id": ObjectId ("599108433268c8e84253be2d")
"name": "bastards"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
}
Example: query the information whose name is Sun San
> db.emp.find ({"name": "Sun San"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be28")
"name": "Sun San"
"sex": "male"
"age": 40
"sal": 2000
"loc": "Shenzhen"
}
Example: query information about men of gender
> db.emp.find ({"sex": "male"}). Pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be26")
"name": "Zhao Yi"
"sex": "male"
"age": 30
"sal": 1000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be28")
"name": "Sun San"
"sex": "male"
"age": 40
"sal": 2000
"loc": "Shenzhen"
}
{
"_ id": ObjectId ("599108423268c8e84253be2b")
"name": "Wu Liu"
"sex": "male"
"age": 30
"sal": 2500
"loc": "Chongqing"
}
{
"_ id": ObjectId ("599108433268c8e84253be2d")
"name": "bastards"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
}
Example: query people over the age of 30
> db.emp.find ({"age": {"$gt": 30}}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be28")
"name": "Sun San"
"sex": "male"
"age": 40
"sal": 2000
"loc": "Shenzhen"
}
{
"_ id": ObjectId ("599108423268c8e84253be2c")
"name": "Zheng Qi"
"sex": "female"
"age": 50
"sal": 4700
"loc": "Chengdu"
}
{
"_ id": ObjectId ("599108433268c8e84253be2d")
"name": "bastards"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
}
Example: inquire about people whose salary is greater than or equal to 4700
> db.emp.find ({"sal": {"$gte": 4700}}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be27")
"name": "Qian er"
"sex": "female"
"age": 22
"sal": 5000
"loc": "Shanghai"
}
{
"_ id": ObjectId ("599108423268c8e84253be29")
"name": "Li Si"
"sex": "female"
"age": 30
"sal": 7000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 6400
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2c")
"name": "Zheng Qi"
"sex": "female"
"age": 50
"sal": 4700
"loc": "Chengdu"
}
{
"_ id": ObjectId ("599108433268c8e84253be2d")
"name": "bastards"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
}
Example: query the name is not Sun San's information
> db.emp.find ({"name": {"$ne": "Sun San"}}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be26")
"name": "Zhao Yi"
"sex": "male"
"age": 30
"sal": 1000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be27")
"name": "Qian er"
"sex": "female"
"age": 22
"sal": 5000
"loc": "Shanghai"
}
{
"_ id": ObjectId ("599108423268c8e84253be29")
"name": "Li Si"
"sex": "female"
"age": 30
"sal": 7000
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 6400
"loc": "Beijing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2b")
"name": "Wu Liu"
"sex": "male"
"age": 30
"sal": 2500
"loc": "Chongqing"
}
{
"_ id": ObjectId ("599108423268c8e84253be2c")
"name": "Zheng Qi"
"sex": "female"
"age": 50
"sal": 4700
"loc": "Chengdu"
}
{
"_ id": ObjectId ("599108433268c8e84253be2d")
"name": "bastards"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
The biggest difference between this time and the previous one is that other JSON structures need to be defined in one JSON structure, and this style remains the same when operating the program.
This is the answer to the question about how to implement relational operation in MongoDB. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.