sudo mongod 启动mongo服务器
mongo 启动mongo
1.导入文件:Bson 文件导入【shell】
mongorestore --collection share_job --db kanche share_job.bson
mongorestore --collection share_job_history --db kanche share_job_history.bson
2.查找&更新
db.share_job.find({"share_account.website":"taoche.com"}) //找淘车
db.share_job.update({_id:ObjectId(
"53ff6c58010000290098c1a8"
)},{
"$set"
:{
"status"
:
"pending"
}})
db.share_job.find({"share_account.website":"iautos.cn", "vehicle.spec.maker":"马自达汽车"}) //找iautos下的制造商为马自达汽车的信息
3.资料参看
3.1索引建立,删除,查询,管理:http://www.cnblogs.com/stephen-liu74/archive/2012/08/01/2561557.html
3.2学习资料:http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html
4.mongo命令操作:http://blog.csdn.net/wuxianglong/article/details/7040151
-------------------数据库操作------------------------
use dolphinop # 创建/切换数据库
showdbs # 查看数据库
showcollections # 查看数据库中的表
db.dropDatabase() # 删除数据库
db.table_name.drop() # 删除表
db.table_name.getIndexes(); # 查看索引
db.table_name.ensureIndex({'name':1}) # 建立索引(1或-1)
-------------------插入操作------------------------
插入数据:db.testcollection.insert({'name':'tompig,'age':25}); 说明:如果testcollection不存在则自动创建。
-------------------查询操作------------------------
查询所有数据:db.testcollection.find();
按条件查询:db.testcollection.find({"name":"li"});
查询统计:db.testcollection.find().count();
按条件查询统计:db.testcollection.find({"name":"liu"}).count();
查询固定条数记录:db.testcollection.find().skip(1).limit(2); 从第二条开始查询查询2条记录。
in查询:db.testcollection.find({"age":{$in:["32","33"]}});
排序查询:db.testcollection.find().sort({"age":-1});从大到小排序
db.user.find('this.age>"31"',{name:1}); 等同于SELECTname FROM user WHERE age >30
-------------------删除操作------------------------
删除所有数据:db.testcollection.remove({});
删除一条符合条件的记录:
(1)db.testcollection.remove({"age":"29"});
(2)db.testcollection.remove({"age":{$lt:"30"}});删除age小于30的记录
说明:$gt: > --(Greaterthan 的首字母)
$gte: >= --(Greaterthan or equal 的首字母)
$lt:< --(Lessthan 的首字母)
$lte:<= --(Lessthan or equal 的首字母)
$ne: != --(Notequal的首字母)
-------------------更新操作------------------------
db.testcollection.update({"name":"liu"},{$set:{"age":"35"}});
等同于sql的:updatetestcollection set 'age'='35' where name='liu';
-------------------函数使用------------------------
db.user.distinct("name",{"age":{$gt:"30"}});
等同mysql的selectdistinct("name") from user where age>"30";
15、pymongo查询排序
mongo的排序:升序:db.feedbacks.find().sort({'id':1})
降序:db.feedbacks.find().sort({'id':-1})
pymongo的排序:db.feedbacks.find().sort('id') # 默认是升序
升序:db.feedbacks.find().sort('id',pymongo.ASCENDING)
将序:db.feedbacks.find().sort('id',pymongo.DESCENDING)
多列排序:db.feedbacks.find().sort([('id',pymongo.ASCENDING),('name',pymongo.DESCENDING)])
添加:db.feedbacks.insert({'id':1,'name':'wuxianglong'})
更新:db.feedbacks.update({'id':1},{'$set':{'name':'wuwenyuan'}})
删除:db.feedbacks.remove() # 删除所有数据
db.feedbacks.remove({'id':1})
5. python操作mongo:【程序】
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Created on 2014-12-26
@author : Jesse
http://www.kanche.com
Mongo operation by python
'''
import pymongo
connection = None
mongoServer = "mongodb://localhost:27017"
def dbOperation():
#create connection:
connection = pymongo.Connection( mongoServer )
#切换到数据库kanche
db = connection.kanche
#切换到collection:share_job ---table
collection = db.share_job
#print:
print '*'*50
#print server_info:
print "\nserver_info:\n",connection.server_info()
#print all db:
allDB = connection.database_names()
print "\nallDB:\n",allDB
#select
num = collection.find({'_id':'ObjectId("545386530100007204e25a88")' }).count()
result = collection.find({'_id':'ObjectId("545386530100007204e25a88")' })
print "\nnum:",num #
print "result:\n",printResult(result)
#update:
#collection.update({'name':'steven1'},{'$set':{'realname':'测试1修改'}}, False,False)
'''
只修改最后一条匹配到的数据
第3个参数设置为True,没找到该数据就添加一条
第4个参数设置为True,有多条记录就不更新
'''
#删除表: connection.drop_database("kanche")
#创建表: user = connection.kanche.users
#clost server
connection.disconnect()
def printResult(rows):
for row in rows:
for key in row.keys(): #遍历字典
print row[key], #加, 不换行打印
print ''
if __name__ == '__main__':
dbOperation()