mongodb是一个非关系性数据库,也被称之为文本数据库,数据库中存储的数据都是JSON格式
1.在DOS系统中找到mongodb文件夹,并启动
指令:mongod.exe -dbpath "D:\mongodb\data"
2.在bin目录下双击mongo.exe打开一个控制端
通用命令(可以通过help查看相关指令)
查看所有数据库:show dbs;
新建数据库(如果当前数据库存在则不会新建而是进入当前数据库,否则新建):use db_name;
查看所有的表(collections相当于table):show collections
查看表中的所有记录:db.collection_name.find();
where查询:
条件是字符型的:db.collection_name.find({name:"xxx"});
Integer类型:db.collection_name.find({age:20});
官方提供的操作语句对照表:
上行:SQL 操作语句
下行:Mongo 操作语句
CREATE TABLE USERS (a Number, b Number)
db.createCollection("mycoll")
INSERT INTO USERS VALUES(1,1)
db.users.insert({a:1,b:1})
SELECT a,b FROM users
db.users.find({}, {a:1,b:1})
SELECT * FROM users
db.users.find()
SELECT * FROM users WHERE age=33
db.users.find({age:33})
SELECT a,b FROM users WHERE age=33
db.users.find({age:33}, {a:1,b:1})
SELECT * FROM users WHERE age=33 ORDER BY name
db.users.find({age:33}).sort({name:1})
SELECT * FROM users WHERE age>33
db.users.find({'age':{$gt:33}})})
SELECT * FROM users WHERE age<33
db.users.find({'age':{$lt:33}})})
SELECT * FROM users WHERE name LIKE "%Joe%"
db.users.find({name:/Joe/})
SELECT * FROM users WHERE name LIKE "Joe%"
db.users.find({name:/^Joe/})
SELECT * FROM users WHERE age>33 AND age<=40
db.users.find({'age':{$gt:33,$lte:40}})})
SELECT * FROM users ORDER BY name DESC
db.users.find().sort({name:-1})
SELECT * FROM users WHERE a=1 and b='q'
db.users.find({a:1,b:'q'})
SELECT * FROM users LIMIT 10 SKIP 20
db.users.find().limit(10).skip(20)
SELECT * FROM users WHERE a=1 or b=2
db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )
SELECT * FROM users LIMIT 1
db.users.findOne()
SELECT DISTINCT last_name FROM users
db.users.distinct('last_name')
SELECT COUNT(*y) FROM users
db.users.count()
SELECT COUNT(*y) FROM users where AGE > 30
db.users.find({age: {'$gt': 30}}).count()
SELECT COUNT(AGE) from users
db.users.find({age: {'$exists': true}}).count()
CREATE INDEX myindexname ON users(name)
db.users.ensureIndex({name:1})
CREATE INDEX myindexname ON users(name,ts DESC)
db.users.ensureIndex({name:1,ts:-1})
EXPLAIN SELECT * FROM users WHERE z=3
db.users.find({z:3}).explain()
UPDATE users SET a=1 WHERE b='q'
db.users.update({b:'q'}, {$set:{a:1}}, false, true)
UPDATE users SET a=a+2 WHERE b='q'
db.users.update({b:'q'}, {$inc:{a:2}}, false, true)
DELETE FROM users WHERE z="abc"
db.users.remove({z:'abc'});
3.对中文乱码的解决
<1>.mongodb使用UTF-8编码,而DOS中默认使用的是GBK.所以对DOS窗口进行编码设置
chcp 65001
<2>.选择设置字体
属性-->字体(除点阵外的所有字体都不会出现乱码,根据自己的喜好选择相应的字体)
4.JAVA DEMO:下载mongo.jar 下载地址:https://github.com/mongodb/mongo-java-driver/downloads
package com.brin.mongo;
import java.net.UnknownHostException;
import java.util.List;
import com.brin.vo.DemoVO;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.util.JSON;
public class Demo {
public static void main(String[] args){
try {
Mongo mg = new Mongo();
DB db = mg.getDB("brin");
DBCollection cbl = db.getCollection("demo");
Demo d = new Demo();
DemoVO vo = new DemoVO();
vo.setId("0000000000000");
vo.setName("test");
vo.setPassword("shanghaiXXX");
vo.setAddress("北京");
vo.setAge(20);
d.add(mg, db, cbl, vo);
d.query();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public void query(){
try {
Mongo mg = new Mongo("127.0.0.1", 27017);
for(String name : mg.getDatabaseNames()){
System.out.println("databaseName:" + name);
DB db = mg.getDB(name);
for(String tableName : db.getCollectionNames()){
System.out.println("tableName:" + tableName + " in " + name);
DBCollection coll = db.getCollection(tableName);
DBCursor cur = coll.find();
while(cur.hasNext()){
System.out.println(cur.next());
}
System.out.println(cur.count());
System.out.println(cur.getCursorId());
System.out.println(JSON.serialize(cur));
}
}
System.out.println("===============查询demo collection中name为test的记录===================");
DB d_b = mg.getDB("brin");
DBCollection col = d_b.getCollection("demo");
BasicDBObject bdb = new BasicDBObject("name","test");
List<DBObject> list = col.find(bdb).toArray();
for(int i = 0; i < list.size(); i ++){
System.out.println(list.get(i));
DemoVO dvo = new DemoVO();
dvo.setName((String)list.get(i).get("name"));
dvo.setId((String)list.get(i).get("id"));
dvo.setAddress((String)list.get(i).get("address"));
System.out.println("=============================dvo.getName:" + dvo.getName() + " id:" + dvo.getId() + " address:" + dvo.getAddress());
}
System.out.println();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public void add(Mongo mg, DB db, DBCollection collection, DemoVO vo){
BasicDBObject demo = new BasicDBObject();
demo.put("id", vo.getId());
demo.put("name", vo.getName());
demo.put("password", vo.getPassword());
demo.put("age", vo.getAge());
demo.put("address", vo.getAddress());
collection.insert(demo);
}
}