我有一份文件---
Employees:[
{
name:"abc",
contact:"123",
email:"xyz@gmail.com"
},
{
name:"efg",
contact:"456",
email:"efg@gmail.com"
},
{
name:"hij",
contact:"789",
email:"hij@gmail.com"
}
]
Run Code Online (Sandbox Code Playgroud)
对于列表中的所有键名,我需要使用value ="abc"更新名称.
我试过更新像
db.collection.update(
{ "_id" : ObjectId("5308595e3256e758757b4d2f") },
{ "$set": { "Employees.name " : "abc" } }
);
Run Code Online (Sandbox Code Playgroud)
但是得到错误:不能使用部件(Employees of Employees.name)来遍历元素.
您好
我正在尝试学习mongo-java driver.i遵循此mongodb文档.以下是我的代码
public class JMongoDBCDemo
{
MongoClient mongoClient;
DB db;
DBCollection coll;
public JMongoDBCDemo()
{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
db = mongoClient.getDB( "messenger" );
coll = db.getCollection("users");
DBObject myDoc = coll.findOne();
System.out.println(myDoc);
mongoClient.close();
System.out.println("Got a collection...");
}
public static void main(String[] args){
JMongoDBCDemo mongoDemo = new JMongoDBCDemo();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是输出
Apr 05, 2015 12:17:47 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Apr 05, 2015 12:17:47 …Run Code Online (Sandbox Code Playgroud) 我有一个mongo查询要执行:
query = { "dateField" : { "$gte" : ISODate('2011-11-10T07:45:32.962Z') } }
Run Code Online (Sandbox Code Playgroud)
当我db.Collection.find(query)在mongo shell上执行操作时,我可以检索结果.
我怎么能用Java查询呢?我尝试根据Date参数构造一个String.但是在构建String的过程中,它最终会被传递"ISODate('2011-11-10T07:45:32.962Z')"而不是ISODate('2011-11-10T07:45:32.962Z')(没有周围的引号).
使用Java API构建此查询的最佳方法是什么?
谢谢 !
我是MongoDB的新手,我正在使用它和Java驱动程序.我有这个文件结构:
{ "_id" : ObjectId("4f7d2ba6fd5a306d82687d48"), "room" : "Den" }
{ "_id" : ObjectId("4f7d2baafd5a306d82687d49"), "room" : "Foyer" }
{ "_id" : ObjectId("4f7d2fdcfd5a306d82687d4a"), "room" : "Master Bedroom" }
{ "_id" : ObjectId("4f7d301afd5a306d82687d4b"), "room" : "Guest Bedroom" }
{ "_id" : ObjectId("4f7d2b98fd5a306d82687d47"), "code" : "A", "lights" : [ { "name" : "Overhead", "code" : "1" } ], "room" : "Kitchen" }
Run Code Online (Sandbox Code Playgroud)
最后一行特别有兴趣说明我想做什么.每个文档都是一个房间,并且可以具有对应于作为子文档阵列的值的"灯"键.从建模的角度来看,我有一个房子,它有0-n个房间,每个房间都有0-n个灯.我想在Java中做的是将房间的名称作为参数,并返回与灯光阵列中的子文档相对应的DBObject集合 - "让我所有灯光为房间'厨房'",例如.
到目前为止,在TDD风格中逐步进行,我构建了这个查询:
public static final String ROOM_KEY = "room";
public static final String EQUALS_KEY = "$eq";
private BasicDBObject buildRoomNameQuery(String roomName) …Run Code Online (Sandbox Code Playgroud) 当我将一个List插入mongodb时,有一个问题:
Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class mongodb.Person
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:259)
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:198)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:142)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:252)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:211)
at com.mongodb.DBCollection.insert(DBCollection.java:57)
at com.mongodb.DBCollection.insert(DBCollection.java:87)
at com.mongodb.DBCollection.save(DBCollection.java:716)
at com.mongodb.DBCollection.save(DBCollection.java:691)
at mongodb.MongoDB.main(MongoDB.java:45)
Run Code Online (Sandbox Code Playgroud)
Person类定义如下:
class Person{
private String name;
public Person(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
该计划是:
DBCollection coll = db.getCollection("test");
DBObject record = new BasicDBObject();
List<Person> persons= …Run Code Online (Sandbox Code Playgroud) 我想用java中的参数执行soem admin命令.
命令是:
{ enablesharding : "test" }
{ shardcollection : "test.test_collection", key : {"number":1} }
Run Code Online (Sandbox Code Playgroud)
我怎么能用java驱动程序呢?
以下代码不起作用:
mongo.getDb("admin").command("{shardcollection : \"test.test_collection\", key:\"number\":1} }")
Run Code Online (Sandbox Code Playgroud) 我正在批量写入MongoDB,并且得到了OOM异常(java.lang.OutOfMemoryError:超出了GC开销限制).我会问两个问题:1.这个OOM是从Mongo客户端驱动程序还是MongoDB服务器发生的?2.是否有一些线索如何发生这种情况?
FO 2016-11-15 15:19:10,437 - [TS] org.mongodb.driver.cluster info(71) - No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, all=[ServerDescription{address=mongo.server1-or:30000, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo.server2:30000, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo.server3:30000, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
INFO 2016-11-15 15:19:11,448 - [TS] org.mongodb.driver.cluster info(71) - No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, all=[ServerDescription{address=mongo.server1-or:30000, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo.server2:30000, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo.server3:30000, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
INFO 2016-11-15 15:19:14,324 - [TS] org.mongodb.driver.cluster info(76) - Exception in monitor …
我想看看 mongo java 驱动程序产生什么查询,但我无法做到这一点。
使用官方文档中的信息,我只能在更新操作执行的日志中看到,但我没有看到此操作的查询。
在阅读了mongo文档后,说明MongoClient的每个实例都处理了自己的池,我怎样才能在整个应用程序中只有一个实例呢?
这似乎可能是使用单例bean的场景,但这似乎会破坏连接池的目的.如果一次只有一个用户能够访问包含MongoClient实例的bean,那么池中的多个连接肯定不会同时使用.
我对单身人士的理解是否错误,或者这确实是正确的方法吗?
我有一个更新的对象/文档列表,我需要立即保存列表中的所有对象。
我在 MongoTemplate 中看到 save() 但它一次只能保存一个文档。有什么方法可以一次保存多个文档,或者我需要调用循环保存?
mongodb mongo-java spring-data-mongodb mongotemplate mongo-java-driver