安装MongODB
vim /etc/yum.repos.d/mongodb-org-4.2.repo
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
yum install -y mongodb-org
vi /etc/mongod.conf
bindIp:0.0.0.0
service mongod restart
firewall-cmd --permanent --zone=public --add-port=27017/tcp
firewall-cmd --reload
- #登录本机或
mongo
use admin
db.createUser({user: “root”,pwd: “123456”, roles: [ { role: “root”, db: “admin” } ]})
mongo --port 27017 -u “root” -p “123456” --authenticationDatabase “admin”
hive 集成MongoDB
wget -P /opt/hive/lib/ https://repo1.maven.org/maven2/org/mongodb/mongo-hadoop/mongo-hadoop-hive/1.5.1/mongo-hadoop-hive-1.5.1.jar
wget -P /opt/hive/lib/ https://repo1.maven.org/maven2/org/mongodb/mongo-hadoop/mongo-hadoop-core/1.5.1/mongo-hadoop-core-1.5.1.jar
wget -P /opt/hive/lib/ https://repo1.maven.org/maven2/org/mongodb/mongo-java-driver/3.2.1/mongo-java-driver-3.2.1.jar
4.进入hive
add jar /opt/hive/lib/mongo-hadoop-core-1.5.1.jar;
add jar /opt/hive/lib/mongo-hadoop-hive-1.5.1.jar;
add jar /opt/hive/lib/mongo-java-driver-3.2.1.jar;
集合操作
db–>:代表当前使用的数据库
1.创建一个数据库(使用时没有就直接创建) use databasename;
方式一:隐式创建集合
当向集合中的插入文档时,如果集合不存在,系统会自动创建,所以向一个不存在的集合中插入数据也就是创建了集合> db test > show tables > db.users.insert({"usernmae": "mengdee", "age": 26}) WriteResult({ "nInserted" : 1 }) > show tables users >
方式二:显示创建集合
db.createCollection(“集合名字”, 可选配置)
显示创建集合可以通过一些配置创建一些特殊的集合,如固定集合> show tables users > db.createCollection("address") { "ok" : 1 } // 固定集合只能通过调用方法显式创建,固定集合可以指定集合存储数据的大小和最多允许存储的条数 // 当固定集合文档条数达到上限时,再插入新的文档会将最老的文档删除掉,然后插入到该位置 > db.createCollection("address", {capped: true, size: 10000, max:1000}) { "ok" : 1 } > show tables address users >
删除集合
db.集合名字.drop()
> db.address.drop() true
查看集合
show tables和show collections都可以查看当前数据下的集合
> show tables address users > > show collections address users >
添加数据
方式一: insert: _id 会自动创建唯一索引,当id重复的时候会报错 db.集合名字.insert({}) // 插入一条,返回值中不包含insertedIds db.集合名字.insert([{}, {}]) // 批量插入,返回值中不包含insertedIds db.集合名字.insertOne(Bson) // 插入一条,返回值返回插入的insertedId db.集合名字.insertMany(Bson) // 批量插入,返回值中包含insertedIds db.集合名字.findAndModify({查询条件}, "update": {需要添加或更新的字段}, "upsert": true }); 写入安全: 应答模式:插入时会返回成功或者失败 非应答模式:插入时没有反馈,即插入有没有成功不知道 > var user = {"name": "mengdee", "age": 20, "address": "上海市浦东新区张江镇", "create_time": new Da te()} > db.users.insert(user) WriteResult({ "nInserted" : 1 }) > db.users.find() { "_id" : ObjectId("5976ad21670af2aa52ea90df"), "username" : "mengdee", "age" : 26 } { "_id" : ObjectId("5976b395670af2aa52ea90e0"), "name" : "mengdee", "age" : 20, "address" : "上海市 浦东新区张江镇", "create_time" : ISODate("2017-07-25T02:57:04.545Z") } > var userDoc = db.users.findOne() > var insertDate = userDoc["_id"] > insertDate.getTimestamp() ISODate("2017-07-25T02:29:53Z") > insertDate.str 5976ad21670af2aa52ea90df > db.users.insertOne({"username": "mengday3"}) { "acknowledged" : true, "insertedId" : ObjectId("5976b632670af2aa52ea90e1") } > db.users.insertMany([{"username": "mengday4"}, {"username": "mengday5"}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("5976b666670af2aa52ea90e2"), ObjectId("5976b666670af2aa52ea90e3") ] } > db.users.insert([{"username": "mengday6"}, {"username": "mengday7"}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) // 使用insertOne插入重复的_id 会报错 > db.users.insertOne({"_id": 1, "username": "mengday8"}) { "acknowledged" : true, "insertedId" : 1 } > db.users.insertOne({"_id": 1, "username": "mengday8"}) 2017-07-25T11:15:47.822+0800 E QUERY [thread1] WriteError: E11000 duplicate key error collection: test.users index: _id_ dup key: { : 1.0 } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.users index: _id_ dup key: { : 1.0 } ", "op" : { "_id" : 1, "username" : "mengday8" } }) // findAndModify 也可以用于插入文档,但是前提是一定不存在,如果存在了就变成更新了,单纯的插入还是不要用种方式了,findAndModify一般用于更新或删除操作 > db.users.findAndModify({ "query": {"username": "mengday11"}, "update": {"username": "mengday11", "age": 26}, "upsert": true }) null > db.users.find() { "_id" : ObjectId("597c584448c373e228a9259e"), "username" : "xxx", "age" : 26 }
MongoDB Shell是一种JS,所以可以通过var来定义变量,可以通过find()方法来查找集合中的所有数据,相当于select * from users
插入数据时,如果没有指定_id这个字段,系统会自动生成一个值,从该值中能够解析出插入文档的日期或者获取日期中的字符串值方式二:save()
db.集合名字.save(Bson) : 如果要插入的文档没有指定_id,则插入文档,如果指定_id,如果集合中不包含_id,则插入文档,如果集合中已经存在相同的id值,则更会整体替换> db.users.find() { "_id" : 1, "username" : "mengday8" } { "_id" : 2, "username" : "mengday8" } > db.users.save({ "_id" : 3, "mengday9" : 20}) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 3 }) > db.users.save({ "_id" : 2, "age" : 20, "gender": 1 }) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday8" } { "_id" : 2, "age" : 20, "gender" : 1 } { "_id" : 3, "mengday9" : 20 } > > db.users.save([{ "_id" : 4, "username": "mengday9"}, { "_id" : 5, "username":"mengday10"}])
方式三: update()
update({查询条件}, {更新的文档}, 是否开启addOrUpdate) : addOrUpdate为true,当集合中不存在的时候就插入,存在就更新> db.users.find() { "_id" : 5, "username" : "mengday10" } > db.users.update({"username" : "mengday11"}, { "_id" : 6, "age" : 20, "gender" : 1 }, true) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 6 }) > db.users.find() { "_id" : 5, "username" : "mengday10" } { "_id" : 6, "age" : 20, "gender" : 1 } >
删除 remove
remove({删除条件}): 删除满足条件的所有数据
remove({删除条件}, true): 删除满足条件的第一条数据
remove({}): 清空集合中的所有文档// 删除bson变量中的某个字段 > var user = {"username": "mengday", "age": 26} > delete user.age > user { "username" : "mengday" } > db.users.find() { "_id" : 1, "username" : "mengday", "password" : "123456" } { "_id" : 2, "username" : "mengday2", "password" : "123456" } { "_id" : 3, "username" : "mengday3", "password" : "123456", "age" : 18 } { "_id" : 4, "username" : "mengday4", "password" : "123456", "age" : 28 } { "_id" : 5, "username" : "mengday5", "password" : "123456", "age" : 38 } > db.users.remove({age: {$lt: 38}}) WriteResult({ "nRemoved" : 2 }) > db.users.find() { "_id" : 1, "username" : "mengday", "password" : "123456" } { "_id" : 2, "username" : "mengday2", "password" : "123456" } { "_id" : 5, "username" : "mengday5", "password" : "123456", "age" : 38 } > db.users.remove({"password": "123456"}, true) WriteResult({ "nRemoved" : 1 }) > db.users.find() { "_id" : 2, "username" : "mengday2", "password" : "123456" } { "_id" : 5, "username" : "mengday5", "password" : "123456", "age" : 38 } > db.users.remove({}) WriteResult({ "nRemoved" : 2 }) // findAndModify可以用来插入或更新upsert、也可以用来删除 > db.users.findAndModify({ "query": {"username": "mengday"}, "remove": true }) { "_id" : ObjectId("597c3c1587d089dfa7ce1be3"), "username" : "mengday", "addresses" : [ { "city" : "shanghai", "area" : "zhangjiang" }, { "city" : "beijing", "area" : "CHAOYANG" } ], "create_time" : ISODate("2017-07-29T09:09:14.031Z") }
更新 update,findAndModify
更新指定字段的值
替换整个文档
更新满足条件的第一条文档
更新满足条件的所有文档> db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456", "age" : 38 } // 使用$set修改器修改指定字段, 当字段不存在时会创建并赋值 > db.users.update({"username": "mengday5"}, {$set: {"age": 18}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456", "age" : 18 } // $unset 用于删除字段 > db.users.update({"username": "mengday5"}, {"$unset": {"age": 1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456" } > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456" } // $push: 向数组的尾部添加一个元素,如果字段不存在则创建 > db.users.update({"username": "mengday5"}, {"$push": {"hobby": "mm"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456", "hobby" : [ "mm" ] } > db.users.update({"username": "mengday5"}, {"$push": {"hobby": "money"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456", "hobby" : [ "mm", "money" ] } > // $push + $each : 批量push > db.users.update({"username": "mengday5"}, {"$push": {"hobby": {"$each": ["play", "eat"]}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456", "hobby" : [ "mm", "money", "play", "eat" ] } > // $pushAll = $push + $each 批量push > db.users.update({"username": "mengday5"}, {"$pushAll": {"hobby": ["drink", "happy"]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456", "hobby" : [ "mm", "money", "play", "eat", "drink", "happy" ] } > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456" } // $addToSet:不重复的set集合 > db.users.update({}, {"$addToSet": {"hobby": "eat"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456", "hobby" : [ "eat" ] } > > db.users.update({}, {"$addToSet": {"hobby": {"$each": ["eat", "drink"]}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456", "hobby" : [ "eat", "drink" ] } > // $pop: 弹出数组的头部元素或尾部元素: -1:头部,1:尾部 > db.users.update({}, {"$pop": {"hobby": 1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday5", "password" : "123456", "hobby" : [ "eat" ] } // $pull: 删除数组中的值 > db.lists.insert({"no": [1, 1, 1, 3]}) WriteResult({ "nInserted" : 1 }) > db.lists.update({}, {"$pull": {"no": 1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.lists.find() { "_id" : ObjectId("597c0a3087d089dfa7ce1be2"), "no" : [ 3 ] } > // 使用小标或者定位操作符$来操作数组 > db.users.find() { "_id" : ObjectId("597c3c1587d089dfa7ce1be3"), "username" : "mengday", "addresses" : [ { "city" : "shanghai", "area" : "zhangjiang" }, { "city" : "be ijing", "area" : "chaoyang" } ] } > // 修改内嵌文档数组中第二个元素的值 > db.users.update({"username": "mengday"}, {"$set": {"addresses.1.area": "chaoyangqu"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.findOne() { "_id" : ObjectId("597c3c1587d089dfa7ce1be3"), "username" : "mengday", "addresses" : [ { "city" : "shanghai", "area" : "zhangjiang" }, { "city" : "beijing", "area" : "chaoyangqu" } ] } // 定位操作符$: 查询条件一般是以数组中的元素为条件,使用$符号作为满足查询条件的第一条文档对应的下标值 > db.users.update({"addresses.city": "beijing"}, {"$set": {"addresses.$.area": "CHAOYANG"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.findOne() { "_id" : ObjectId("597c3c1587d089dfa7ce1be3"), "username" : "mengday", "addresses" : [ { "city" : "shanghai", "area" : "zhangjiang" }, { "city" : "beijing", "area" : "CHAOYANG" } ] } // 文档整体替换 > db.users.update({"username": "mengday5"}, {"age": 17}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "age" : 17 } // 第三个参数: 插入或者更新,当_id不存在的时候插入,当_id值存在的时候更新 > db.users.update({"_id": 2}, {"username": "mengday", "age": 16}, true) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 2 }) > db.users.find() { "_id" : 1, "age" : 17 } { "_id" : 2, "username" : "mengday", "age" : 16 } // 更新 > db.users.update({"_id": 2}, {"username": "mengday2", "birthday": new Date()}, true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "age" : 17 } { "_id" : 2, "username" : "mengday2", "birthday" : ISODate("2017-07-25T06:33:10.579Z") } > db.users.find() { "_id" : 1, "username" : "mengday", "age" : 16 } { "_id" : 2, "username" : "mengday2", "age" : 16 } // 更新满足条件的第一条文档 > db.users.update({"age": 16}, {$set: {"age": 18}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.find() { "_id" : 1, "username" : "mengday", "age" : 18 } { "_id" : 2, "username" : "mengday2", "age" : 16 } // 第三个参数:insertOrUpdate, 第四个参数:是否批量更新,true就是更新所有满足条件的文档 > db.users.update({"age": {$gte: 16}}, {$set: {"age": 25}}, false, true) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.users.find() { "_id" : 1, "username" : "mengday", "age" : 25 } { "_id" : 2, "username" : "mengday2", "age" : 25 } > // 查询然后更新,更新是整体替换, 如果想更新指定的字段使用$set修改器即可 > db.users.findAndModify({ query: { "username": "mengday"}, update:{ "age": 20, "username": "mengday20" } }) { "_id" : 1, "username" : "mengday", "age" : 20, "birthday" : ISODate("2017-07-25T07:05:28.286Z") } > db.users.find() { "_id" : 1, "age" : 20, "username" : "mengday20" }
查询
// 查询所有, 相当于 select * from users > db.users.find() { "_id" : 1, "username" : "mengday", "age" : 20, "birthday" : ISODate("2017-07-25T07:05:28.286Z") } { "_id" : 2, "username" : "tom", "age" : 18, "birthday" : ISODate("2017-07-25T07:05:52.166Z") } { "_id" : 3, "username" : "xiaohong", "age" : 28, "birthday" : ISODate("2017-07-25T07:06:13.741Z") } { "_id" : 4, "username" : "xiaoming", "age" : 27, "birthday" : ISODate("2017-07-25T07:06:44.812Z") } { "_id" : 5, "username" : "sunday", "age" : 37, "birthday" : ISODate("2017-07-25T07:07:45.420Z") } > // pretty() 用于格式化查询的结果 > db.users.find().pretty() { "_id" : 1, "username" : "mengday", "age" : 20, "birthday" : ISODate("2017-07-25T07:05:28.286Z") } ... { "_id" : 5, "username" : "sunday", "age" : 37, "birthday" : ISODate("2017-07-25T07:07:45.420Z") } > // 查询一条, 相当于select * from users limit 1 > db.users.findOne() { "_id" : 1, "username" : "mengday", "age" : 20, "birthday" : ISODate("2017-07-25T07:05:28.286Z") } // 查询条件, 相当于 select * from users where age < 20 > db.users.find({"age": {$lt: 20}}) { "_id" : 2, "username" : "tom", "age" : 18, "birthday" : ISODate("2017-07-25T07:05:52.166Z") } // 查询指定的字段,1:代表要查询的字段,0:代表不要查询的字段 ,相当于 select username, age from users where age < 20 > db.users.find({"age": {$lt: 20}}, {"_id":0, "username": 1, "age": 1}) { "username" : "tom", "age" : 18 } // and 条件,多个调剂直接用逗号分开,不需要什么操作符: 相当于 select * from users where age < 20 and id < 3 > db.users.find({"age": {$lt: 30}, "_id": {$lt: 3 }}) { "_id" : 1, "username" : "mengday", "age" : 20, "birthday" : ISODate("2017-07-25T07:05:28.286Z") } { "_id" : 2, "username" : "tom", "age" : 18, "birthday" : ISODate("2017-07-25T07:05:52.166Z") } // 同一个字段多个条件: 相当于 select * from users where age > 25 and age < 30 > db.users.find({"age": {$gt: 25, $lt:30 }}) { "_id" : 3, "username" : "xiaohong", "age" : 28, "birthday" : ISODate("2017-07-25T07:06:13.741Z") } { "_id" : 4, "username" : "xiaoming", "age" : 27, "birthday" : ISODate("2017-07-25T07:06:44.812Z") } // or: 相当于 select * from users where age > 30 or username = 'tom' > db.users.find({$or: [{"age": {$gt: 30}}, {"username": "tom"}]}) { "_id" : 2, "username" : "tom", "age" : 18, "birthday" : ISODate("2017-07-25T07:05:52.166Z") } { "_id" : 5, "username" : "sunday", "age" : 37, "birthday" : ISODate("2017-07-25T07:07:45.420Z") } // and or 混合使用,相当于 select * from users where id < 4 and (username = 'mengdat' or age < 20) > db.users.find({ $or: [{"username": "mengday"}, {"age": {$lt: 20}}], "_id": {$lt: 4} }) { "_id" : 1, "username" : "mengday", "age" : 20, "birthday" : ISODate("2017-07-25T07:05:28.286Z") } { "_id" : 2, "username" : "tom", "age" : 18, "birthday" : ISODate("2017-07-25T07:05:52.166Z") } > // in: 相当于 select * from users where age in (18, 28) > db.users.find({"age": {$in: [18, 28]}}) { "_id" : 2, "username" : "tom", "age" : 18, "birthday" : ISODate("2017-07-25T07:05:52.166Z") } { "_id" : 3, "username" : "xiaohong", "age" : 28, "birthday" : ISODate("2017-07-25T07:06:13.741Z") } > // 正则表达式不但可以匹配字符串还可以匹配字段值是正则表达式类型的,此时是相等匹配 // 模糊查询,正则表达式: 以xiao开头,以ng结尾 // 相当于 select * from users where username like 'xiao%' and username like '%ng' > db.users.find({"username": /^xiao/, "username": /ng$/}) { "_id" : 3, "username" : "xiaohong", "age" : 28, "birthday" : ISODate("2017-07-25T07:06:13.741Z") } { "_id" : 4, "username" : "xiaoming", "age" : 27, "birthday" : ISODate("2017-07-25T07:06:44.812Z") } > db.users.insert({"_id": 6, "username": "sunday", "age": 39, "birthday": new Date(), "hobby": ["eat ", "drink", "play", "happy", "money", "mm"] }) WriteResult({ "nInserted" : 1 }) // 正则表达式忽略大小写 > db.users.find({"username": {$regex:/sunday/, $options:"$i"}}) { "_id" : 5, "username" : "sunday", "age" : 37, "birthday" : ISODate("2017-07-25T07:07:45.420Z") } { "_id" : 6, "username" : "SunDay", "age" : 39, "birthday" : ISODate("2017-07-25T07:53:16.072Z"), "h obby" : [ "eat", "drink", "play", "happy", "money", "mm" ] } // 正则表达式用于数组 > db.users.find({"hobby": {$regex: "mm"}}) { "_id" : 6, "username" : "SunDay", "age" : 39, "birthday" : ISODate("2017-07-25T07:53:16.072Z"), "h obby" : [ "eat", "drink", "play", "happy", "money", "mm" ] } // 正则表达式包含变量时需要使用eval()函数来计算 > var username = "sunday" > db.users.find({"username": {$regex:eval("/" + username + "/i")}}) { "_id" : 5, "username" : "sunday", "age" : 37, "birthday" : ISODate("2017-07-25T07:07:45.420Z") } { "_id" : 6, "username" : "SunDay", "age" : 39, "birthday" : ISODate("2017-07-25T07:53:16.072Z"), "h obby" : [ "eat", "drink", "play", "happy", "money", "mm" ] } // 数组字段: 值,意思就是这个数组中是否包含该元素,如果包含就是满足条件的 > db.food.insert({"fruit": ["apple", "banana", "cherry"]}) WriteResult({ "nInserted" : 1 }) > db.food.find({"fruit": "banana"}) { "_id" : ObjectId("597d353be210addac88b2a36"), "fruit" : [ "apple", "banana", "cherry" ] } // 当值是一个值时是是否包含,当值是数组时就是精确匹配了,此时匹配不到结果 > db.food.find({"fruit": ["apple", "cherry"]}) // $all: 数组中同时都包含多个元素 > db.food.find({"fruit": {$all: ["apple", "cherry"]}}) { "_id" : ObjectId("597d353be210addac88b2a36"), "fruit" : [ "apple", "banana", "cherry" ] } // 查询数组中指定的下标对应的值是否和给的值一样 // 查询文档中的fruit中第三个元素的值是cherry的文档 > db.food.find({"fruit.2": "cherry"}) { "_id" : ObjectId("597d353be210addac88b2a36"), "fruit" : [ "apple", "banana", "cherry" ] } > // $size: 根据数组的长度进行筛选 > db.food.find({"fruit": {$size: 3}}) { "_id" : ObjectId("597d353be210addac88b2a36"), "fruit" : [ "apple", "banana", "cherry" ] } > db.food.find() { "_id" : ObjectId("597d353be210addac88b2a36"), "fruit" : [ "apple", "banana", "orange", "cherry" ] > // 返回数组中前两个元素 > db.food.find({}, {"fruit": {$slice: 2}}) { "_id" : ObjectId("597d353be210addac88b2a36"), "fruit" : [ "apple", "banana" ] } // 返回数组中后两个元素 > db.food.find({}, {"fruit": {$slice: -2}}) { "_id" : ObjectId("597d353be210addac88b2a36"), "fruit" : [ "orange", "cherry" ] } // 偏移下标为1,取2个长度,相当于 limint 0, 2 > db.food.find({}, {"fruit": {$slice: [1, 2]}}) { "_id" : ObjectId("597d353be210addac88b2a36"), "fruit" : [ "banana", "orange" ] } > > db.test.find() { "_id" : ObjectId("597d4342e210addac88b2a37"), "x" : [ 2, 5 ] } { "_id" : ObjectId("597d4350e210addac88b2a38"), "x" : [ 4, 5 ] } { "_id" : ObjectId("597d4367e210addac88b2a39"), "x" : [ 4, 10 ] } // $elemMatch: 数组中是否有一个元素同时满足所有条件(只要有一个元素满足就能匹配上) > db.test.find({"x": {"$elemMatch": {$gt: 5, $lt: 20}}}) { "_id" : ObjectId("597d4367e210addac88b2a39"), "x" : [ 4, 10 ] } // 根据字段的数据类型来查询,2代码String > db.users.find({"username": {$type: 2}}) { "_id" : 1, "username" : "mengday", "age" : 20, "birthday" : ISODate("2017-07-25T07:05:28.286Z") } { "_id" : 2, "username" : "tom", "age" : 18, "birthday" : ISODate("2017-07-25T07:05:52.166Z") } { "_id" : 3, "username" : "xiaohong", "age" : 28, "birthday" : ISODate("2017-07-25T07:06:13.741Z") } { "_id" : 4, "username" : "xiaoming", "age" : 27, "birthday" : ISODate("2017-07-25T07:06:44.812Z") } { "_id" : 5, "username" : "sunday", "age" : 37, "birthday" : ISODate("2017-07-25T07:07:45.420Z") } { "_id" : 6, "username" : "SunDay", "age" : 39, "birthday" : ISODate("2017-07-25T07:53:16.072Z"), "h obby" : [ "eat", "drink", "play", "happy", "money", "mm" ] } // 查询文档中没有username字段的或者username的值是null > db.users.find({"username": null}) // 查询文档中存在username字段,并且值是null, $in:[null]和username:null 一样的效果 > db.users.find({"username": {$in: [null], $exists: true}}) { "_id" : ObjectId("597c58f848c373e228a925a6"), "username" : null, "age" : 25 } { "_id" : ObjectId("597c591e48c373e228a925ab"), "username" : null, "age" : 24 } // 自定义筛选条件,通过js函数返回的boolean值来筛选,可以实现复杂条件的筛选 > db.users.find({$where: function(){ return this.username == 'mengday' }}) { "_id" : 1, "username" : "mengday", "age" : 20, "birthday" : ISODate("2017-07-25T07:05:28.286Z") } // 运行命令: 平常使用的很多命令如db.test.drop()底层都是调用的db.runCommand({"函数名":参数值})来实现的 > db.runCommand({"drop": "test"}) { "ns" : "test.test", "nIndexesWas" : 1, "ok" : 1 } // db.getLastError 和 db.runCommand({getLastError: 1}) 是一样的 > db.getLastError > db.runCommand({getLastError: 1}) ### 游标 游标: 将查询结果赋值给一个局部变量,这个局部变量就是游标, 使用游标时shell并不理解查询数据库,而是等待真正开始要求获取结果时(调用hasNext()或者next()方法)才发送查询,这样在执行之前可以给查询附加额外的选项,如skip、limit、sort等,游标是一次性的,即用过一次即销毁
mongodb与hive的集成
在 Hive 中创建 MongoDB 关联表:
--创建表 CREATE TABLE eventlog ( id string, userid string, type string, objid string, time string, source string ) STORED BY 'com.mongodb.hadoop.hive.MongoStorageHandler' WITH SERDEPROPERTIES ('mongo.columns.mapping'='{"id":"_id"}') TBLPROPERTIES ('mongo.uri'='mongodb://192.168.126.135:27017/hive.eventlog'); ('mongo.uri'='mongodb://username:password@ip:port/xxx.xx xxxx'); --在猛odb中插入数据 var even=({userid:"101",type:"error",time:new Date(),source:"mongodb"}) db.hive.eventlog(even)