我有一个具有唯一键的Collection,当我批量插入此集合时,它会取消插入过程。就像这样: 收集中的数据:
{
"app_key" : "key",
"insert_date" : "1438963200",
"uuid" : "123"
}
唯一的关键是:app_key
+ uuid
然后我插入这样的数据:
{
"app_key" : "key",
"insert_date" : "1438963200",
"uuid" : "1234"
}
{
"_id" : ObjectId("55c80dbf1a4a5fa61c03b503"),
"app_key" : "key",
"insert_date" : "1438963200",
"uuid" : "123"
}
{
"app_key" : "key",
"insert_date" : "1438963200",
"uuid" : "234"
}
结果收藏是
{
"app_key" : "key",
"insert_date" : "1438963200",
"uuid" : "123"
}
{
"app_key" : "key",
"insert_date" : "1438963200",
"uuid" : "1234"
}
所以它在uuid:123
对象取消,这是mongo的问题还是bug ...
谢谢。
答案 0 :(得分:1)
我认为param可以帮助解决这个问题,如下所示:
WriteResult wr = dbc.insert(obj1,new WriteConcern(0, 0, false, false, true));
if (null == wr.getError()) {
System.out.println("Insert Success");
}
作为函数定义:
/**
* Creates a WriteConcern object.
* <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
* <p> w represents the number of servers:
* <ul>
* <li>{@code w=-1} None, no checking is done</li>
* <li>{@code w=0} None, network socket errors raised</li>
* <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
* <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
* </ul>
* </p>
* @param w number of writes
* @param wtimeout timeout for write operation
* @param fsync whether or not to fsync
* @param j whether writes should wait for a journaling group commit
* @param continueOnInsertError if batch inserts should continue after the first error
*/
public WriteConcern( int w , int wtimeout , boolean fsync , boolean j, boolean continueOnInsertError) {
_w = w;
_wtimeout = wtimeout;
_fsync = fsync;
_j = j;
_continueOnErrorForInsert = continueOnInsertError;
}