您好,登錄后才能下訂單哦!
MongoDB中怎么實現關系運算,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
在MongoDB里面支持的關系查詢操作:大于($gt),小于($lt),大于等于($gte),小于等于($lte),不等于($ne),等于(key:value,$eq)。
范例:定義一個員工信息集合
db.emp.drop();
db.emp.insert({"name":"趙一","sex":"男","age":30,"sal":1000,"loc":"北京"});
db.emp.insert({"name":"錢二","sex":"女","age":22,"sal":5000,"loc":"上海"});
db.emp.insert({"name":"孫三","sex":"男","age":40,"sal":2000,"loc":"深圳"});
db.emp.insert({"name":"李四","sex":"女","age":30,"sal":7000,"loc":"北京"});
db.emp.insert({"name":"周五","sex":"女","age":30,"sal":6400,"loc":"北京"});
db.emp.insert({"name":"吳六","sex":"男","age":30,"sal":2500,"loc":"重慶"});
db.emp.insert({"name":"鄭七","sex":"女","age":50,"sal":4700,"loc":"成都"});
db.emp.insert({"name":"王八","sex":"男","age":35,"sal":8000,"loc":"北京"});
> db.emp.find().pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "趙一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "錢二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "周五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吳六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重慶"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
范例:查詢姓名為孫三的信息
> db.emp.find({"name":"孫三"}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
范例:查詢性別為男的人員信息
> db.emp.find({"sex":"男"}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "趙一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吳六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重慶"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
范例:查詢年齡大于30的人員
> db.emp.find({"age":{"$gt":30}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be28"),
"name" : "孫三",
"sex" : "男",
"age" : 40,
"sal" : 2000,
"loc" : "深圳"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
范例:查詢薪水大于等于4700的人員
> db.emp.find({"sal":{"$gte":4700}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "錢二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "周五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
}
范例:查詢姓名不是孫三的信息
> db.emp.find({"name":{"$ne":"孫三"}}).pretty();
{
"_id" : ObjectId("599108423268c8e84253be26"),
"name" : "趙一",
"sex" : "男",
"age" : 30,
"sal" : 1000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be27"),
"name" : "錢二",
"sex" : "女",
"age" : 22,
"sal" : 5000,
"loc" : "上海"
}
{
"_id" : ObjectId("599108423268c8e84253be29"),
"name" : "李四",
"sex" : "女",
"age" : 30,
"sal" : 7000,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2a"),
"name" : "周五",
"sex" : "女",
"age" : 30,
"sal" : 6400,
"loc" : "北京"
}
{
"_id" : ObjectId("599108423268c8e84253be2b"),
"name" : "吳六",
"sex" : "男",
"age" : 30,
"sal" : 2500,
"loc" : "重慶"
}
{
"_id" : ObjectId("599108423268c8e84253be2c"),
"name" : "鄭七",
"sex" : "女",
"age" : 50,
"sal" : 4700,
"loc" : "成都"
}
{
"_id" : ObjectId("599108433268c8e84253be2d"),
"name" : "王八",
"sex" : "男",
"age" : 35,
"sal" : 8000,
"loc" : "北京"
此時與之前最大的區(qū)別就在于,在一個JSON結構里面需要定義其他的JSON結構,并且這種風格在進行程序操作的時候依然如此。
關于MongoDB中怎么實現關系運算問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。