1 MongoDB C 驱动编译安装
3 使用C驱动
3 遇到的坑
mongoc_read_prefs_t* read_prefs = mongoc_read_prefs_new(MONGOC_READ_NEAREST); bson_t* tag = bson_new(); mongoc_read_prefs_add_tag(read_prefs, tag); bson_destroy(tag); mongoc_cursor_t* cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, query, NULL, read_prefs);
bson_t* tag = bson_new(); BSON_APPEND_UTF8(tag, "location", "bj"); mongoc_read_prefs_add_tag(read_prefs, tag); bson_destroy(tag);
bson_t* tag = bson_new(); BSON_APPEND_UTF8(tag, "location", "bj"); mongoc_read_prefs_add_tag(read_prefs, tag); mongoc_read_prefs_add_tag(read_prefs, NULL); bson_destroy(tag);
4 其它驱动
#!/home/work/bin/python # test find with tag_set # cswuyg @ 2014.7.18 # install pymongo pre. import pymongo import time REMOTE_ADDRESS = "xxxxhost" REMOTE_PORT = 27030 def _test_find(): f_w = open("test", ‘w‘) #client = pymongo.MongoClient(REMOTE_ADDRESS, 27030, readPreference=‘nearest‘) rpre = pymongo.read_preferences.Secondary(tag_sets = [{‘location‘: ‘gz‘}]) client = pymongo.MongoClient(REMOTE_ADDRESS, REMOTE_PORT, read_preference=rpre) # 注意这里的read_preference s = time.time() docs = client.myapp.myuser.find({‘name‘: ‘cswuyg‘}) with open(‘tmp.txt‘, ‘w‘) as f_w: for item in docs: f_w.write(str(item)) f_w.write(‘\n‘) e = time.time() print(e - s) if __name__ == "__main__": _test_find()
/* * 测试 mongodb 驱动 耗时 * cswuyg @ 2015.12.29 */ "use strict"; var mongodb = require(‘mongodb‘); var assert = require(‘assert‘); var fs = require(‘fs‘); var url = ‘mongodb://xxxhost:27030/myapp?w=1&readPreference=nearest&readPreferenceTags=location:bj‘; //在uri上设置读取优先级和tag var findDocuments = function(collection, callback) { collection.find({‘name‘: ‘cswuyg‘}).limit(1).toArray(function(err, docs) { callback(docs); }); } mongodb.MongoClient.connect(url, function(err, db) { assert.equal(null, err); console.log("Connected correctly to server"); var col = db.collection(‘myuser‘); var s = new Date().getTime(); findDocuments(col, function(docs) { var writerStream = fs.createWriteStream(‘tmp.txt‘); writerStream.write(JSON.stringify(docs[0]), ‘UTF8‘); writerStream.end(); var e = new Date().getTime(); console.log(e - s); }); });
/* * * 测试 mongoose 耗时 * * cswuyg @ 2015.12.29 * */ "use strict"; var mongoose = require(‘mongoose‘); var fs = require(‘fs‘); var connect = mongoose.createConnection(‘mongodb://xxxhost:27030, yyyhost:27030/myapp‘, {mongos: true}); //注意mongos var schema = {name: {type: String}}; var colSchema = new mongoose.Schema(schema, {collection: ‘myuser‘}); var model = connect.model(‘myuser‘, colSchema); setTimeout(function() { var s = new Date().getTime(); var query = new mongoose.Query({‘name‘: ‘cswuyg‘}).read(‘n‘, [{location:‘nj‘}]); //Query层面设置读取优先级和tag model.find(query).exec(function(err, doc) { var writerStream = fs.createWriteStream(‘output.txt‘); writerStream.write(doc.toString(), ‘UTF8‘); writerStream.end(); var e = new Date().getTime(); console.log(e - s); }); }, 1000); setTimeout(function() { var s = new Date().getTime(); var query = new mongoose.Query({‘name‘: ‘cswuyg‘}).read(‘n‘, [{location:‘nj‘}]); model.find(query).exec(function(err, doc) { var writerStream = fs.createWriteStream(‘output.txt‘); writerStream.write(doc.toString(), ‘UTF8‘); writerStream.end(); var e = new Date().getTime(); console.log(e - s); }); }, 2000);
原文:http://www.cnblogs.com/cswuyg/p/5087377.html