MongoDB 3.6 资料汇总
来源:互联网 发布:北京数据库培训机构 编辑:程序博客网 时间:2024/06/08 11:54
Linux 下源代码按照步骤 查看
官方JAVA API资料查看
修改默认IP,运行外部请求访问,使用 –bind_ip_all参数配合启动
mongod --bind_ip_all
- json转bson,附上官方的例子原始连接
String inputFilename; // initialize to the path of the file to read fromMongoCollection<Document> collection; // initialize to the collection to which you want to writeBufferedReader reader = new BufferedReader(new FileReader(inputFilename));try { String json; while ((json = reader.readLine()) != null) { collection.insertOne(Document.parse(json)); } } finally { reader.close();}
cluster搭建教程,按照此篇在MongoDB 3.6中有所改变
- rs1 rs2 启动脚本修改为如下
###for rs1$BIN_HOME/mongod --port $RS1_1_PORT --dbpath $RS1_1_DB_PATH --fork --logpath $RS1_1_DB_LOG --replSet $RS1 --smallfiles --nojournal --shardsvr$BIN_HOME/mongod --port $RS1_2_PORT --dbpath $RS1_2_DB_PATH --fork --logpath $RS1_2_DB_LOG --replSet $RS1 --smallfiles --nojournal --shardsvr $BIN_HOME/mongod --port $RS1_3_PORT --dbpath $RS1_3_DB_PATH --fork --logpath $RS1_3_DB_LOG --replSet $RS1 --smallfiles --nojournal --shardsvr### for rs2$BIN_HOME/mongod --port $RS2_1_PORT --dbpath $RS2_1_DB_PATH --fork --logpath $RS2_1_DB_LOG --replSet $RS2 --smallfiles --nojournal --shardsvr$BIN_HOME/mongod --port $RS2_2_PORT --dbpath $RS2_2_DB_PATH --fork --logpath $RS2_2_DB_LOG --replSet $RS2 --smallfiles --nojournal --shardsvr$BIN_HOME/mongod --port $RS2_3_PORT --dbpath $RS2_3_DB_PATH --fork --logpath $RS2_3_DB_LOG --replSet $RS2 --smallfiles --nojournal --shardsvr
- 解决错误 Attempting to initiate a replica set with name xxx, but command line xxx
rs.initiate(config){ "ok" : 0, "errmsg" : "Attempting to initiate a replica set with name rs2, but command line reports rs1; rejecting", "code" : 93, "codeName" : "InvalidReplicaSetConfig"}
查询资料得知,当前mongod 配合 –replSet 参数里的 数值一定要和config参数里的一致,不然就会报错
我原来的是这个,指定的是rs1,但config文件里,和实际运行的节点里写的是rs2,因为不一致所以导致了这个错误
把下面的rs1修改为rs2(我的实际replSet 名字)即可
/usr/local/mongodb/bin/mongod --port 27020 --dbpath /home/mongo_db/data/rs1_3 --fork --logpath /home/mongo_db/log/rs1_3.log --replSet rs1 --smallfiles --nojournal
- 报错 mirrored config server connections are not supported,这里是国外解决方法原帖
简单来说就是把启动config server的脚本修改如下
###for statup the config server$BIN_HOME/mongod --port $CONF1_PORT --dbpath $CONF1_DB_PATH --fork --logpath $CONF1_DB_LOG --configsvr --replSet conf$BIN_HOME/mongod --port $CONF2_PORT --dbpath $CONF2_DB_PATH --fork --logpath $CONF2_DB_LOG --configsvr --replSet conf$BIN_HOME/mongod --port $CONF3_PORT --dbpath $CONF3_DB_PATH --fork --logpath $CONF3_DB_LOG --configsvr --replSet conf
另外需要连接到任意一台的config server上 执行命令注册 config server的repl集群
###因为是单机,多实例环境,所以省略了IP地址,直接指定了端口mongo --port $CONF1_PORT
//登陆以后,执行一下命令,注册名为conf的config集群.rs.initiate( { _id: "conf", configsvr: true, members: [ { _id : 0, host : "localhost:40000" }, { _id : 1, host : "localhost:40001" }, { _id : 2, host : "localhost:40002" } ] })
然后启动route,这次不会报错了
mongos --configdb "conf/localhost:40000,localhost:40001,localhost:40002" --fork --logpath $ROUTE_DB_LOG --port $ROUTE_PORT --bind_ip_all
- java客户端 连接 cluster的时候报错 Can not create shards: sharding not enabled for db
记得在mogos,也就是route上,执行如下命令,your_db_name填你自己的实际数据库名.
sh.enableSharding("your_db_name")
java端 简单实例代码
/* * Copyright 2015 MongoDB, 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. */package tour;import com.mongodb.Block;import com.mongodb.MongoClient;import com.mongodb.MongoClientURI;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoCursor;import com.mongodb.client.MongoDatabase;import com.mongodb.client.model.BulkWriteOptions;import com.mongodb.client.model.DeleteOneModel;import com.mongodb.client.model.InsertOneModel;import com.mongodb.client.model.ReplaceOneModel;import com.mongodb.client.model.UpdateOneModel;import com.mongodb.client.model.WriteModel;import com.mongodb.client.result.DeleteResult;import com.mongodb.client.result.UpdateResult;import org.bson.Document;import java.util.ArrayList;import java.util.List;import static com.mongodb.client.model.Accumulators.sum;import static com.mongodb.client.model.Aggregates.group;import static com.mongodb.client.model.Aggregates.match;import static com.mongodb.client.model.Aggregates.project;import static com.mongodb.client.model.Filters.and;import static com.mongodb.client.model.Filters.eq;import static com.mongodb.client.model.Filters.exists;import static com.mongodb.client.model.Filters.gt;import static com.mongodb.client.model.Filters.gte;import static com.mongodb.client.model.Filters.lt;import static com.mongodb.client.model.Filters.lte;import static com.mongodb.client.model.Projections.excludeId;import static com.mongodb.client.model.Sorts.descending;import static com.mongodb.client.model.Updates.inc;import static com.mongodb.client.model.Updates.set;import static java.util.Arrays.asList;import static java.util.Collections.singletonList;/** * The QuickTour code example see: https://mongodb.github.io/mongo-java-driver/3.0/getting-started */public class QuickTour { /** * Run this main method to see the output of this quick example. * * @param args takes an optional single argument for the connection string */ public static void main(final String[] args) { MongoClient mongoClient; if (args.length == 0) { // connect to the local database server mongoClient = new MongoClient(); } else { mongoClient = new MongoClient(new MongoClientURI(args[0])); } // get handle to "mydb" database MongoDatabase database = mongoClient.getDatabase("mydb"); // get a handle to the "test" collection MongoCollection<Document> collection = database.getCollection("test"); // drop all the data in it collection.drop(); // make a document and insert it Document doc = new Document("name", "MongoDB") .append("type", "database") .append("count", 1) .append("info", new Document("x", 203).append("y", 102)); collection.insertOne(doc); // get it (since it's the only one in there since we dropped the rest earlier on) Document myDoc = collection.find().first(); System.out.println(myDoc.toJson()); // now, lets add lots of little documents to the collection so we can explore queries and cursors List<Document> documents = new ArrayList<Document>(); for (int i = 0; i < 100; i++) { documents.add(new Document("i", i)); } collection.insertMany(documents); System.out.println("total # of documents after inserting 100 small ones (should be 101) " + collection.count()); // find first myDoc = collection.find().first(); System.out.println(myDoc.toJson()); // lets get all the documents in the collection and print them out MongoCursor<Document> cursor = collection.find().iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } for (Document cur : collection.find()) { System.out.println(cur.toJson()); } // now use a query to get 1 document out myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson()); // now use a range query to get a larger subset cursor = collection.find(gt("i", 50)).iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } // range query with multiple constraints cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } // Query Filters myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson()); // now use a range query to get a larger subset Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; collection.find(gt("i", 50)).forEach(printBlock); // filter where; 50 < i <= 100 collection.find(and(gt("i", 50), lte("i", 100))).forEach(printBlock); // Sorting myDoc = collection.find(exists("i")).sort(descending("i")).first(); System.out.println(myDoc.toJson()); // Projection myDoc = collection.find().projection(excludeId()).first(); System.out.println(myDoc.toJson()); // Aggregation collection.aggregate(asList( match(gt("i", 0)), project(Document.parse("{ITimes10: {$multiply: ['$i', 10]}}"))) ).forEach(printBlock); myDoc = collection.aggregate(singletonList(group(null, sum("total", "$i")))).first(); System.out.println(myDoc.toJson()); // Update One collection.updateOne(eq("i", 10), set("i", 110)); // Update Many UpdateResult updateResult = collection.updateMany(lt("i", 100), inc("i", 100)); System.out.println(updateResult.getModifiedCount()); // Delete One collection.deleteOne(eq("i", 110)); // Delete Many DeleteResult deleteResult = collection.deleteMany(gte("i", 100)); System.out.println(deleteResult.getDeletedCount()); collection.drop(); // ordered bulk writes List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>(); writes.add(new InsertOneModel<Document>(new Document("_id", 4))); writes.add(new InsertOneModel<Document>(new Document("_id", 5))); writes.add(new InsertOneModel<Document>(new Document("_id", 6))); writes.add(new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2)))); writes.add(new DeleteOneModel<Document>(new Document("_id", 2))); writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4))); collection.bulkWrite(writes); collection.drop(); collection.bulkWrite(writes, new BulkWriteOptions().ordered(false)); //collection.find().forEach(printBlock); // Clean up database.drop(); // release resources mongoClient.close(); }}
阅读全文
0 0
- MongoDB 3.6 资料汇总
- MongoDB & Redis资料汇总
- MongoDB资料汇总
- MongoDB资料汇总专题
- MongoDB资料汇总
- MongoDB资料汇总
- MongoDB资料汇总专题
- MongoDB资料汇总专题
- MongoDB资料汇总专题
- MongoDB资料汇总专题
- MongoDB资料汇总专题
- MongoDB资料汇总专题
- MongoDB资料汇总专题
- MongoDB资料汇总专题
- 转 MongoDB资料汇总专题
- Redis资料汇总专题 MongoDB资料汇总专题-----ChinaUnix
- 最近整理的学习资料(Hadoop/MongoDB/ActiveMQ)汇总
- MongoDB资料
- linux--如何查看文件内容具体的某一行数据
- Android webView 调试
- Unity之SerializeField(序列化字段)
- BASIC-10十进制转十六进制
- 中企动力助天正生物争夺破伤风抗毒素全球话语权
- MongoDB 3.6 资料汇总
- XFermode图表
- 发大水范德萨看
- 面试官这么表现就有戏?不一定哦!
- NRF51822芯片参数及模块介绍
- [Unity问题]The file "MemoryStream" is corrupted! Remove it and launch unity again!
- QLineEdit的setInputMask和正则表达式冲突
- My97的WdatePicker({dateFmt:'yyyy-MM'})兼容ie9
- Linux入门——虚拟机安装CentOS