保存为bat方便:
"C://Program Files//MongoDB//Server//3.2//bin//mongod.exe" --dbpath=D://corp//db/
多条数据更新:
db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})
多条数据更新:
var cursor = db.getCollection('land_info').find({});
tt = 10000;
while (cursor.hasNext()) {
obj = cursor.next();
printjson(obj._id);
db.getCollection('land_info').update({"_id" : obj._id},{"$set":{land:"vv"+tt}})
tt++;
}
var cursor = db.getCollection('land_info').find({"ZLDWMC":"吉文镇吉文镇"});
while (cursor.hasNext()) {
obj = cursor.next();
db.getCollection('land_info').update({"_id" : obj._id},{"$set":{SHAPE_AREA:obj.SHAPE_AREA *1}})
}
根据条件转移数据库
var temp1 = db.land_info.find({landname:"NS11"});
while(temp1.hasNext()) db.tab2.insert(temp1.next());
db.land_info.find().forEach(
function (elem) {
db.land_info.update(
{
_id: elem._id
},
{
$set: {
OBJECTID: elem.OBJECTID + ''
}
}
);
}
);
更改字段名:
db.land_info.update({},{$rename:{"appkey":"appid"}},{multi:true})
mongoose返回结果添加字段: From: https://cnodejs.org/topic/546e95a9c4922d383a829768
User.find({age: 24}, function(error, users){
console.log(users);
users.forEach(function(user, index){
user._doc.name = 'Test' + index;
});
console.log(users);
});
查找不存在字段:
db.land_info.update({fields:{$exists: false}},{$rename:{"appkey":"appid"}},{multi:true})
db.land_info.aggregate([{$group : {_id : "null",num_tutorial : {$sum : "$SHAPE_AREA"}}}])
db.land_info.aggregate([{ $match : { "landname":"yilizhen" } },{$group : {_id : "$ZLDWMC",num_tutorial : {$sum : "$SHAPE_AREA"}}}])
数据聚合
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
唯一索引
db.person.ensureIndex({"name":1},{"unique":true})
地理位置查询
http://www.cnblogs.com/yu-zhang/p/5064005.html
http://coderschool.cn/1783.html
http://docs.mongoing.com/manual-zh/tutorial/query-a-2dsphere-index.html
数据导出json:
mongoexport -h 127.0.0.1 -d 数据库名 -c 表名 -o 文件名
数据导入json:
mongoimport -h 127.0.0.1 -d 数据库名 -c 表名 –-type json –-file ./land.json