mongodb命令使用

最近学习mongodb的使用,整理了一些常用命令
windows服务安装:mongod --install --serviceName MongoDB --serviceDisplayName MongoDB --logpath c:\data\log\MongoDB.Log --dbpath c:\data\db --directoryperdb  
启动命令: mongod 
连接 :mongo
数据库默认路径c:\data\db c:\data\log

删除服务:

show dbs:显示数据库列表 
show collections:显示当前数据库中的集合(类似关系数据库中的表) 
show users:显示用户

运行数据库并导入demo数据
  1. 运行cmd,输入命令启动mongo服务:mongod --dbpath E:\QuestionMakerDB
  2. 导入demo数据:在cmd中运行:mongorestore -d QuestionMaker --drop E:\QuestionMaker\backup\mongodb\QuestionMaker

1、查询所有记录
db.userInfo.find();

3、查询age = 22的记录
db.userInfo.find({"age": 22});

db.posts.insert({"title":"Second Post", "user": "alice"})
db.posts.update({
"user": "alice"
}, {
"title": "Second Post",
"user": "alice"
}, {
upsert: true
})


db.posts.find({ "user": { $in: ["alice", "bob"] } })

db.posts.find({ "user": "alice" })
db.posts.find({ "user": "alice", "commentsCount": { $gt: 10 } })
db.posts.find( { $or: [{ "user": "alice" }, { "user": "bob" }] })


--默认更新一行,设置multi才能更新多行
db.posts.update({
"user": "alice"
}, {
$set: {
"title": "Second Post"
}
}, {
multi: true
})


db.posts.remove({ "user": "alice" })
db.posts.remove({ "user": "alice" }, true)

mongoose
mongodb://username:password@hostname:port/database

db.getCollection('articles').find({"content.first":"fff"})

合并查询
db.getCollection('document').find({$and:[{'Rows.Cells.Field.@Name':/病人出生地/},{'Rows.Cells.Field.Text':/龙岩/}]})







posted @ 2015-10-15 11:32  .windy  阅读(171)  评论(0编辑  收藏  举报