我想用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) 我试图通过使用Joda Time将本地日期转换为UTC .我使用的代码如下所示,效果很好.
Date localDate = new Date();
System.out.println("Local Date : " + localDate);
DateTimeZone tz = DateTimeZone.getDefault();
Date utcDate = new Date(tz.convertLocalToUTC(localDate.getTime(), false));
System.out.println("UTC Date : " + utcDate);
Output :
Local Date : Wed May 29 11:54:46 EEST 2013
UTC Date : Wed May 29 08:54:46 EEST 2013
Run Code Online (Sandbox Code Playgroud)
但是,如果我将UTC日期作为参数发送到DateTimeZone.convertLocalToUTC()方法,它也会将小时减少3.但是,由于它是UTC日期,我希望它不会再次转换日期.这是一个错误还是我错过了什么?
Date localDate = new Date();
System.out.println("Local Date : " + localDate);
DateTimeZone tz = DateTimeZone.getDefault();
Date utcDate = new Date(tz.convertLocalToUTC(localDate.getTime(), false));
System.out.println("UTC Date : " …Run Code Online (Sandbox Code Playgroud) 在我的Java应用程序中,我将mongoDB中的值存储在ArrayList(Java对象集)中.如何从DBObject检索数据
我将数据存储在mongoDB中,如下所示:
{ "students" : [{"firstName" : "Jesse", "lastName" : "Varnell", "age" : "15", "gender" : "M" }, { "firstName" : "John", "lastName" : "Doe", "age" : "13", "gender" : "F"}] }
Run Code Online (Sandbox Code Playgroud)
我正在为学生提供Java对象:
public class Student {
public String firstName;
public String lastName;
public String age;
public String gender; // M, F
}
Run Code Online (Sandbox Code Playgroud)
我正在从mongoDB中检索数据,如:
BasicDBObject query = new BasicDBObject();
query.put("user", username);
DBCursor cursor = theCollection.find(query);
while (cursor.hasNext()) {
DBObject theObj = cursor.next();
//How to get the DBObject value to ArrayList …Run Code Online (Sandbox Code Playgroud) 我有下面的代码有效:
if (aDBCursor.hasNext()) {
DBObject aDbObject = aDBCursor.next();
aDbObject.put("title", "Test Title");
ArrayList<DBObject> department = new ArrayList<DBObject>();
DBObject nested1 = new BasicDBObject();
nested1.put("name", "Department A");
nested1.put("id", 1);
department.add(nested1);
DBObject nested2 = new BasicDBObject();
nested2.put("name", "Department B");
nested2.put("id", 2);
department.add(nested2);
aDbObject.put("department", department);
collection.save(aDbObject);
}
Run Code Online (Sandbox Code Playgroud)
但是我在地图中有A部和B部的数据,如:
Map<Object,Object> map = new HashMap<Object,Object>();
map.put("1", "Department A");
map.put("2", "Department B");
Run Code Online (Sandbox Code Playgroud)
保存这些数据的最佳/最简单方法是什么?有没有办法将地图直接放入mongo DB?或者我是否必须在地图上循环?
进入地图的数据已经从数据库中获取,如下所示:
String[] values = req.getParameterValues("departments");
Map<Object,Object> map = new HashMap<Object,Object>();
DBCollection collection = database.getCollection("Departments");
BasicDBObject query = new BasicDBObject();
query.put("id", new BasicDBObject("$in", values));
DBCursor …Run Code Online (Sandbox Code Playgroud) 我正在使用带有java和MongoDB的mongo-jackson-wrapper.我通过查询我的一个字段(而不是_id字段)找到一个对象,然后我需要知道_id字段值,无论结果是更新还是插入.但是,我得到一个例外:
com.mongodb.MongoException: No objects to return
at net.vz.mongodb.jackson.WriteResult.getSavedId(WriteResult.java:97)
Run Code Online (Sandbox Code Playgroud)
异常来自包装器,而不是MongoDB驱动程序本身.
WriteResult<EntityDocument, String> wr
= coll.update(DBQuery.is("corefEntityId", corefEntityId), up, true, false);
Run Code Online (Sandbox Code Playgroud)
什么(如果有的话)是正确的方法吗?
在Java中,我有一个类Num,以及一些扩展的类Num,比如Num_int和Num_double.我想知道方法是否可以识别给定Num对象是否是一个Num_int.
我有以下代码:
void test(Num_int x) {
System.out.println("int");
} // test
void test(Num x) {
System.out.println("other");
} // test
Num_int A = new Num_int( );
Num B = new Num_int( );
Num C = new Num_double( );
test(A); // prints "int"
test(B); // prints "other"
test(C); // prints "other"
Run Code Online (Sandbox Code Playgroud)
不幸的是,当给出A作为参数时,方法"test"仅打印"int".当B通过时,我也不能打印"int",因为B是通过创建的Num B = new Num_int( );.这可能吗?
谢谢.
我有一个文档中MongoDB的
name: name
date_created: date
p_vars: {
01: {
a: a,
b: b,
}
02: {
a: a,
b: b,
}
....
}
Run Code Online (Sandbox Code Playgroud)
表示为 DBObject
key,value对都是类型String DBObjectas as JSONfile?我有一个JUnit rule称为MongoRule样子
public class MongoRule extends ExternalResource {
private static final Logger LOGGER = LoggerFactory.getLogger(MongoRule.class);
private final MongoService mongoService;
public MongoRule() throws UnknownHostException {
mongoService = new MongoService(getConfiguredHost(), getConfiguredPort(), getConfiguredDatabase());
}
@Override
protected void before() throws Throwable {
LOGGER.info(" Setting up Mongo Database - " + getConfiguredDatabase());
}
@Override
protected void after() {
LOGGER.info("Shutting down the Mongo Database - " + getConfiguredDatabase());
mongoService.getMongo().dropDatabase(getConfiguredDatabase());
}
@Nonnull
public DB getDatabase() {
return mongoService.getMongo().getDB(getConfiguredDatabase());
}
@Nonnull
public Mongo getMongo() …Run Code Online (Sandbox Code Playgroud) 我们有一个系统,使用户能够在其应用程序上创建应用程序并存储数据.我们想要分离每个应用程序的索引.我们为每个应用程序创建一个核心,并在用户进行查询时搜索给定的应用程序.由于应用程序之间没有任何关系,因此该解决方案可以比将所有索引存储在一起更好.
我有两个与此相关的问题.
嗨,我有这样的数据模型
module{
name: "xx",
sa: [
{
sa_name: "yy",
fact: [
fact_name: "zz"
],
dim: [
dim_name: "qq"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我有一个嵌入模块和事实和暗淡嵌入sa.
我试过db.coll.find({"module.sa.fact.name":"zz"},{})不工作,因为单巢db.coll.find({"module.sa.name":"yy"},{})工作正常.如何在子文档中查询此子文档.