MongoDB Note
来源:互联网 发布:怎么复制文件到mac 编辑:程序博客网 时间:2024/05/14 16:34
参考资料:
https://docs.mongodb.com/manual/text-search/
http://www.runoob.com/mongodb/mongodb-tutorial.html
可视化工具:robomongo
实例–数据库–集合
不需设置相同字段,相同字段也不需相同数据类型
文档就是一行,文档组就是集合
不能有重复的键
区分类型和大小写
用的bjson,类似json
初学指令
use databasedbshow dbs show tablesdb.collection.insert({...}) save:指定_id为更新db.collection.find() .pretty()toInsert=文档 db.collection.insert(toInsert)db.dropDatabase()db.collection.drop()//全部更新db.collection.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}}) ,{multi:true})//文档替换文档db.collection.save()//删除对应全部文档db.col.remove({'title':'MongoDB 教程'})//删除第一条对应文档db.col.remove(DELETION_CRITERIA,1)
基本查询:
等于:db.col.find({"title":"菜鸟教程"}).pretty()//where likes <50小于:db.col.find({"likes":{$lt:50}}).pretty()同理:gt lte gte ne
或:db.col.find({$or:[{"by":"菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty()与:db.col.find({"by":"菜鸟教程", "title":"MongoDB 教程"}).pretty()与或混合:db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty()//查出title为string的数据db.col.find({"title" : {$type : 2}})
限制查询条数:db.COLLECTION_NAME.find().limit(NUMBER)某些数据的第几条到第几条:db.COLLECTION_NAME.find().skip(10).limit(100)//排序db.col.find({},{"title":1,_id:0}).sort({"likes":-1})无论位置先后: sort再skip再limit升序降序1/-1:db.col.find({},{"title":1,_id:0}).sort({"likes":-1})建立升序索引:db.col.ensureIndex({"title":1}, {background: true})
聚合:db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}]) 类似: select by_user, count(*) from mycol group by by_userdb.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}]) 类似:select by_user as _id, count(*) as num_tutorial from mycol group by by_user
mongo添加账户:
http://ibruce.info/2015/03/03/mongodb3-auth/
use admindb.createUser( { user: "buru", pwd: "12345678", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
java连接mongodb测试:
参考:http://blog.csdn.net/ererfei/article/details/50857103
import java.util.ArrayList; import java.util.List;import org.bson.Document;import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress;import com.mongodb.client.FindIterable;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoCursor;import com.mongodb.client.MongoDatabase; public class Connect { public static void main(String[] args){ try { //连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址 //ServerAddress()两个参数分别为 服务器地址 和 端口 ServerAddress serverAddress = new ServerAddress("localhost",27017); List<ServerAddress> addrs = new ArrayList<ServerAddress>(); addrs.add(serverAddress); //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码 MongoCredential credential = MongoCredential.createScramSha1Credential("root", "test1", "123abc".toCharArray()); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); credentials.add(credential); //通过连接认证获取MongoDB连接 MongoClient mongoClient = new MongoClient(addrs,credentials); //连接到数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("test1"); System.out.println("Connect to database successfully "+mongoDatabase.getName()); // //创建集合// mongoDatabase.createCollection("CreateByJava"); // System.out.println("Collection created successfully"); //插入文档 /** * 1. 创建文档 org.bson.Document 参数为key-value的格式 * 2. 创建文档集合List<Document> * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document) * */ MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("CreateByJava"); Document document = new Document("title", "MongoDB"). append("description", "database"). append("likes", 100). append("by", "Fly"); List<Document> documents = new ArrayList<Document>(); documents.add(document); mongoCollection.insertMany(documents); System.out.println("Document inserted successfully"); //检索所有文档 /** * 1. 获取迭代器FindIterable<Document> * 2. 获取游标MongoCursor<Document> * 3. 通过游标遍历检索出的文档集合 * */ FindIterable<Document> findIterable = mongoCollection.find(); MongoCursor<Document> mongoCursor = findIterable.iterator(); while(mongoCursor.hasNext()){ System.out.println(mongoCursor.next()); } } catch (Exception e) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } }
阅读全文
0 0
- MongoDB Note
- MongoDB Note
- note
- note
- NOTE
- note
- Note
- note
- note
- Note
- note
- Note
- Note
- note
- Note
- Note
- Note
- note
- ios 检查系统是否连接了蓝牙设备
- 第一次学习笔记,Makefile和uboot的一点东西
- 欢迎使用CSDN-markdown编辑器
- 【剑指offer】翻转单词顺序列
- Integer和int之间的==比较
- MongoDB Note
- shell中嵌套expect实现ssh登录
- Composer进阶使用
- 算法大法之--顺序表
- JVM原理和优化
- C++ 异或运算
- HoloLens开发学习笔记(一):HoloLens简介
- BI团队如何高效应对快速扩张的公司的需求 —— 阿里云MVP赵玮主题分享【2】
- Android Butter Knife(小刀)的注解