mongodb

2017-05-18  本文已影响0人  Mr_码客
1.mongodb安装
mongod.exe --dbpath c:\data\db

mongodb成功开启:
默认配置:
host:127.0.0.1
port:27017

链接:http://pan.baidu.com/s/1jHHG5zK 密码:3pbi

2.{ read: 'secondaryPreferred' , bufferCommands: false}
//API调用记录
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

//API旧库
var conn = mongoose.createConnection("mongodb://");

var apicallhistorySchema = mongoose.Schema({
    user_id: { type: String, index: true },
    product_name: { type: String },
 
}, { read: 'secondaryPreferred' , bufferCommands: false});

module.exports = conn.model('apicallhistories', apicallhistorySchema);

secondaryPreferred:读写分离;
bufferCommands:等待太久,没有then,也没有catch,false,等待太久,强制结束,throw err。

3.aggregate管道聚合
var date = new Date('2017-2-22');
var time = new Date('2017-2-1');

db.apicallhistories.aggregate(
        [
            { $match : { created_date : { $gt :time ,$lte : date} } },
           
            {$group:{_id :{user_id:"$user_id",method_name:"$method_name",status:"$status"},count:{$sum:1},
                    totalcost:{$sum:"$cost"},avg_timespent:{$avg:"$time_spent"},max_timespent:{$max:"$time_spent"},min_timespent:{$min:"$time_spent"}
                
            }}
        ]
    )
4.find
db.enterprises.find({licenses_alls:{$elemMatch:{$ne:null}}})
db.enterprises.find({licenses_alls:{$ne:null}})
db.enterprises.find({judicial_freezes_alls:null})
db.enterprises.find({judicial_freezes_alls:{"$size":0}})
db.C.find({"c":null})
db.C.find({"c":{"$in":[null],"$exists":true}})
5.update
db.batchjobhistories.updateMany({name:"apiHistoriesToOts(test)"},{$set:{milestone:""}},{ multi: true,upsert: false});
- **更新满足多个件的记录**

var arr = ["56dd8ea15288f730bda692a2",
"56dd8ea15288f730bda692a7",
"56dd8f735288f730bda692a8",
"56dd8f954c6880c8b99787c7"]
db.apiwritefailedhistories.update( { "id" : {$in: arr}} , { $set : { "condition" : "true"} }, {upsert: true} )

#####5.$and、$or

db.enterprises.find({},{$or: [{resolveStatus: "待处理"}, {resolveStatus: "已完成"}], $and: [{zentao_task_id: {$ne: null}}, {zentao_task_id: {$ne: ""}}]})

上一篇下一篇

猜你喜欢

热点阅读