db.users.({:,age:25}) not 显示not ,代表当前从节点只读. --进入9929端口,可。数据库编程——MongoDB json( 七 )。" />

数据库编程——MongoDB json( 七 )


users
--查看数据
:> db.users.find()
{ "_id" : ("f587"), "" : "yekai", "age" : 34 }
--从节点执行操作
:> db.users.({:'',age:25})
not
显示not ,代表当前从节点只读.
--进入9929端口,可以看到仲裁节点的提示符号
@:~$ mongo :9929
shell : 2.4.9
to: :9929/test
:>
》执行故障转移测试:
1)可以杀掉9927的主节点
--再在从节点执行一下查询,发现从节点意见自己变成主节点
:> db.users.find()
{ "_id" : ("f587"), "" : "yekai", "age" : 34 }
--再次执行插入操作
:> db.users.({:'',age:25})
:> db.users.find()
{ "_id" : ("f587"), "" : "yekai", "age" : 34 }
{ "_id" : ("fa09"), "" : "", "age" : 25 }
已经可以插入成功
2)再将9927上线
--9927将变为从节点
@:~$ mongo :9927
shell : 2.4.9
to: :9927/test
:> show ;
Sun Sep 4 13:55:33.333 error: { "$err" : "notand =false", "code" : 13435 } at src/mongo/shell/query.js:128
:> rs.()
:> show
.
users
--查看数据
:> db.users.find()
{ "_id" : ("f587"), "" : "yekai", "age" : 34 }
{ "_id" : ("fa09"), "" : "", "age" : 25 }
仲裁节点的作用是协调选举,监测系统运行状态,提供节点互相通讯的数据信息 。
11、API 使用
》文件准备:
\\mongo\\mongo文件夹及目录下pch.h、.h、.h
/** @file pch.h : include file for standard system include files,*or project specific include files that are used frequently, but*are changed infrequently*//*Copyright 2009 10gen Inc.**Licensed under the Apache License, Version 2.0 (the "License");*you may not use this file except in compliance with the License.*You may obtain a copy of the License at**http://www.apache.org/licenses/LICENSE-2.0**Unless required by applicable law or agreed to in writing, software*distributed under the License is distributed on an "AS IS" BASIS,*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*See the License for the specific language governing permissions and*limitations under the License.*/#ifndef MONGO_PCH_H#define MONGO_PCH_H// our #define macros must not be active when we include// system headers and boost headers#include "mongo/client/undef_macros.h"#include "mongo/platform/basic.h"#include #include #include #include #include #include #include #include #include #include #include #include #include "time.h"#include "string.h"#include "limits.h"#define BOOST_FILESYSTEM_VERSION 3//modify by yekai 20161104#include #include #include #include #include #include "mongo/client/redef_macros.h"#include "mongo/util/exit_code.h"namespace mongo {using namespace std;using boost::shared_ptr;void dbexit( ExitCode returnCode, const char *whyMsg = "" );/**this is here so you can't just type exit() to quit the programyou should either use dbexit to shutdown cleanly, or ::exit to tell the system to quitif you use this, you'll get a link error since mongo::exit isn't defined*/void exit( ExitCode returnCode );bool inShutdown();}#include "mongo/util/assert_util.h"#include "mongo/util/debug_util.h"#include "mongo/util/goodies.h"#include "mongo/util/allocator.h"#include "mongo/util/log.h"#endif // MONGO_PCH_H
pch.h
/** @file server.hThis file contains includes commonly needed in the server files (mongod, mongos, test).It is *NOT* included in the C++ client; i.e. this is a very good place for global-ish things that you don't need to be in the client lib.Over time we should move more here, and more out of pch.h.And get rid of pch.h at some point.*/#pragma once#if !defined(MONGO_EXPOSE_MACROS)# error this file is for mongo server programs not client lib#endif#include #include #include #include "bson/inline_decls.h"//using namespace std;//using namespace bson;/* Note: do not clutter code with these -- ONLY use in hot spots / significant loops. */// branch prediction.indicate we expect to be true#define likely MONGO_likely// branch prediction.indicate we expect to be false#define unlikely MONGO_unlikely// prefetch data from memory//#define PREFETCH MONGOPREFETCH// logs at most once per secs#define LOGATMOST(secs) static time_t __last = 0; time_t __now=time(0); if(__last+secs>__now) {} else if ( ( __last = __now ) > 0 ) log() // log but not too fast.this is rather simplistic we can do something fancier later#define LOGSOME LOGATMOST(20)