How to resolve com.mongodb.WriteConcernException

Refresh

December 2018

Views

743 time

2

I have a document in one collection. I'm trying to update that document by adding one field. But I'm facing the below error.

Exception in thread "main" com.mongodb.WriteConcernException: { "serverUsed" : "localhost:27017" , "err" : "invalid operator: $numberLong" , "code" : 10068 , "n" : 0 , "connectionId" : 279 , "ok" : 1.0}
    at com.mongodb.CommandResult.getWriteException(CommandResult.java:90)

Can anyone please help me to solve this error?

package com.sample.dataload;

import java.net.UnknownHostException;
import java.util.Set;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.sample.dao.impl.mongo.MongoConnection;
import com.sample.dao.impl.mongo.MongoConstants;

public class Test
{
    public static void main(String[] args) throws UnknownHostException
    {
        MongoConnection mongoConnection = new MongoConnection();
        MongoClient mongoClient = mongoConnection.getMongoClient();
        DB sourceDb = mongoClient.getDB(MongoConstants.SOURCE_DB);
        Set<String> sourceCollections = sourceDb.getCollectionNames();
        System.out.println(sourceCollections);
        for (String eachCollection : sourceCollections)
        {
            System.out.println(eachCollection);
            DBCollection collection = sourceDb.getCollection(eachCollection);
            DBCursor cursor = collection.find(new BasicDBObject());
            while (cursor.hasNext())
            {
                DBObject doc = cursor.next();
                BasicDBObject updatedObj = new BasicDBObject().append(MongoConstants.SET, new BasicDBObject().append(DataLoadConstants.IS_PROCESSED, "true"));
                collection.update(doc, updatedObj);
                break;
            }
        }
    }
}

1 answers

2

Одна проблема , которую я вижу в том , что вам не нужно прохождения целого docв первом аргументе updateметода:

collection.update(new BasicDBObject().append("_id", doc._id), updatedObj);