MongoDb

http://www.mongodb.org/

MongoDB学习笔记

http://www.cnblogs.com/lipan/archive/2011/03/08/1966463.htm

 

MongoDB深究之ObjectId
http://www.cnblogs.com/xjk15082/archive/2011/09/18/2180792.html

MongoDB aggregate() .$unwind
http://stackoverflow.com/questions/22663706/mongodb-aggregate-unwind

http://www.json.cn/

python3 selenium动态获取唯品会商品 
http://www.cnblogs.com/nima/p/5604593.html

Aggregation Examples
http://api.mongodb.com/python/current/examples/aggregation.html

"D:\mongodb\mongodb2.6\bin\mongod" --dbpath "D:\mongodb\mongodb2.6\data\db"

mongod --logpath "D:\mongodb\mongodb2.6\data\log\logs.txt" --logappend --dbpath "D:\mongodb\mongodb2.6\data\db" --directoryperdb --serviceName "MongoDB" --serviceDisplayName "MongoDB" --install

返回某些字段
db.test.find({"data.merchandiseList.items.name":"威斯汀酒店风格全棉可水洗羽丝绒矮枕74*48cm(单只装)"},{"data.merchandiseList.items.name":1})

db.test.insert({names:["a","d","e","c"]});
db.test.find({},{names:{$slice:1}}) //返回第一个

返回数组中满足条件的...然而只返回了一条。。。
db.test.find({"data.merchandiseList.items.name":"威斯汀酒店风格全棉可水洗羽丝绒矮枕74*48cm(单只装)"},{"data.merchandiseList.items.$":1})

这种写法会导致全部记录返回
db.test.find({},{"questions.questionEntry.questionItem.theQuestion" : "q1"});

管道,先拆分数组,然后过滤
db.test.aggregate([
{$unwind:"$items"},
{$match:{
        'items.name': /^1.5米床/
    }
}])

或者1.5米床或者全棉
db.test.aggregate([
{$unwind:"$items"},
{$project:{'items.name':1,'items.sell_price':1,'items.stock':1}},
{$match:{
       $and: [{'items.name': /[1.5米床,全棉]/},{'items.stock':'0'} ]
    }
}
])

1.5米床并且全棉
db.test.aggregate([
{$unwind:"$items"},
{$project:{'items.name':1,'items.sell_price':1,'items.stock':1}},
{$match:{
       'items.name': /(?=.*1.5米床)(?=.*全棉)/
    }
}
])

升序
db.test.aggregate([
{$unwind:"$items"},
{$project:{'items.name':1,'items.show_title':1,'items.sell_price':1,'items.list_img':1}},
{$match:{
       'items.name': /(?=.*1.5米床)(?=.*四件套)/
    }
},
{$sort:{'items.sell_price':1}}
])

降序
db.test.aggregate([
{$unwind:"$items"},
{$project:{'items.name':1,'items.show_title':1,'items.sell_price':1,'items.list_img':1}},
{$match:{
       'items.name': /(?=.*1.5米床)(?=.*四件套)/
    }
},
{$sort:{'items.sell_price':-1}}
])

db.test2.aggregate([
{$unwind:"$data.merchandiseList.items"},
{$project:{'data.merchandiseList.items.name':1,'data.merchandiseList.items.show_title':1,'data.merchandiseList.items.sell_price':1,'data.merchandiseList.items.list_img':1}},
{$match:{
       'data.merchandiseList.items.name': /^1.5/
    }
},
{$sort:{'data.merchandiseList.items.sell_price':-1}}
])


db.test2.aggregate([
{$unwind:"$data.merchandiseList.items"},
{$project:{'_id':0,'data.merchandiseList.items.name':1,'data.merchandiseList.items.show_title':1,'data.merchandiseList.items.sell_price':1,'data.merchandiseList.items.list_img':1}},
{$match:{
       'data.merchandiseList.items.name': /(?=.*1.5)(?=.*贡缎)/
    }
},
{$sort:{'data.merchandiseList.items.sell_price':-1}}
])
等同于
db.test2.aggregate([
{"$unwind":"$data.merchandiseList.items"},
{"$project":{'_id':0,'data.merchandiseList.items.id':1,'data.merchandiseList.items.brand_name':1,'data.merchandiseList.items.detail_url':1
    ,'data.merchandiseList.items.show_title':1,'data.merchandiseList.items.sell_price':1,'data.merchandiseList.items.list_img':1}},
{"$match":{
       'data.merchandiseList.items.show_title': /(?=.*1.5)(?=.*贡缎)/
    }
},
{"$sort":{'data.merchandiseList.items.sell_price':-1}}
])

1.pymongo
pprint.pprint(db.test2.find_one())

2.mongodb存储过程
http://www.cnblogs.com/greenteaone/p/5175859.html

db.system.js.find();
db.system.js.save({_id:"getCount",value:function(){return db.test2.count();},description:'获取课程数'});
db.eval("getCount()");

db.system.js.save({_id:"getTest2",value:function(str){
    // str = "/(?=.*1.5)(?=.*贡缎)/";
     result =  db.test2.aggregate([
{"$unwind":"$data.merchandiseList.items"},
{"$project":{'_id':0,'data.merchandiseList.items.id':1,'data.merchandiseList.items.brand_name':1,'data.merchandiseList.items.detail_url':1
    ,'data.merchandiseList.items.show_title':1,'data.merchandiseList.items.sell_price':1,'data.merchandiseList.items.list_img':1}},
{"$match":{
       'data.merchandiseList.items.show_title': "/(?=.*1.5)(?=.*贡缎)/"
    }
},
{"$sort":{'data.merchandiseList.items.sell_price':-1}}
]); 
return result;
},description:'获取商品'});

3.db.test3.remove()
  remove needs a query
  
  db.test3.drop()
  db.test3.remove({})
  
4.distinct  不能多个字段
  The first argument to the distinct command must be a string but was a undefined 
  correct:
  db.test3.distinct('data.merchandiseList.items.show_title')
  wrong:
  db.test3.distinct({'data.merchandiseList.items.id','data.merchandiseList.items.show_title'})

  
db.system.js.save({_id:"getTest",value:function(str){
    // str = "/(?=.*1.5)(?=.*贡缎)/";
    var rst = db.test2.aggregate([
{"$unwind":"$data.merchandiseList.items"},
{"$project":{'_id':0,'data.merchandiseList.items.id': 1,'data.merchandiseList.items.brand_name':1,'data.merchandiseList.items.detail_url':1
    ,'data.merchandiseList.items.show_title':1,'data.merchandiseList.items.sell_price':1,'data.merchandiseList.items.list_img':1}},
{"$match":{
       'data.merchandiseList.items.show_title': /(?=.*1.5)(?=.*贡缎)/
    }
},
{"$sort":{'data.merchandiseList.items.sell_price':-1}}
]);
var addRecord = function(value){ //print(value.data.merchandiseList.items.sell_price); 
    if(db.test3.find({'id':value.data.merchandiseList.items.id,'sell_price':value.data.merchandiseList.items.sell_price}).count()==0)
    db.test3.insert({'id':value.data.merchandiseList.items.id,'brand_name':value.data.merchandiseList.items.brand_name,'detail_url':value.data.merchandiseList.items.detail_url
        ,'show_title':value.data.merchandiseList.items.show_title,'sell_price':value.data.merchandiseList.items.sell_price,'list_img':value.data.merchandiseList.items.list_img}); 
    }
db.test3.drop()    
rst.forEach(addRecord)
return db.test3.find();
},description:'获取商品'});

 

posted on 2013-10-23 17:25  白马酒凉  阅读(100)  评论(0编辑  收藏  举报

导航