Announcement Announcement Module
Collapse
No announcement yet.
spring-data-mongodb-examples-hello,Update().push() not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-data-mongodb-examples-hello,Update().push() not working

    I just clone http://github.com/SpringSource/sprin...t-examples.git.
    I want to test push a pojo to a collection, so i modified a testcase in SimpleMongoTest in spring-data-mongodb-examples-hello project. When I run it ,it throw an exception:
    java.lang.IllegalArgumentException: can't serialize class org.springframework.data.mongodb.examples.hello.do main.Account
    at org.bson.BSONEncoder._putObjectField(BSONEncoder.j ava:213)
    at org.bson.BSONEncoder.putMap(BSONEncoder.java:253)
    at org.bson.BSONEncoder._putObjectField(BSONEncoder.j ava:179)
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:12 3)
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:69 )
    at com.mongodb.OutMessage.putObject(OutMessage.java:1 89)
    at com.mongodb.DBApiLayer$MyCollection.update(DBApiLa yer.java:333)
    at com.mongodb.DBCollection.update(DBCollection.java: 145)
    at com.mongodb.DBCollection.updateMulti(DBCollection. java:169)
    at org.springframework.data.document.mongodb.MongoTem plate$13.doInCollection(MongoTemplate.java:846)
    at org.springframework.data.document.mongodb.MongoTem plate$13.doInCollection(MongoTemplate.java:842)
    at org.springframework.data.document.mongodb.MongoTem plate.execute(MongoTemplate.java:315)
    at org.springframework.data.document.mongodb.MongoTem plate.updateMulti(MongoTemplate.java:842)
    at org.springframework.data.document.mongodb.MongoTem plate.updateMulti(MongoTemplate.java:835)
    at org.springframework.data.mongodb.examples.hello.Si mpleMongoTest.updatingDocuments(SimpleMongoTest.ja va:135)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod .evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.e valuate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements .RunBeforeTestMethodCallbacks.evaluate(RunBeforeTe stMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements .RunAfterTestMethodCallbacks.evaluate(RunAfterTest MethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements .SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.runChild(SpringJUnit4ClassRunner.jav a:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner. java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:184)
    at org.springframework.test.context.junit4.statements .RunBeforeTestClassCallbacks.evaluate(RunBeforeTes tClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements .RunAfterTestClassCallbacks.evaluate(RunAfterTestC lassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.ja va:236)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.run(SpringJUnit4ClassRunner.java:180 )
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)


    this is the test case,
    Code:
    @Test
    	public void updatingDocuments() {
    
    		Person p1 = new Person("Bob", 33);
    		p1.addAccount(new Account("198-998-2188", Account.Type.SAVINGS, 123.55d));
    		mongoTemplate.insert(p1);
    		Person p2 = new Person("Mary", 25);
    		p2.addAccount(new Account("860-98107-681", Account.Type.CHECKING, 400.51d));
    		mongoTemplate.insert(p2);
    		Person p3 = new Person("Chris", 68);
    		p3.addAccount(new Account("761-002-8901", Account.Type.SAVINGS, 10531.00d));
    		mongoTemplate.insert(p3);
    		Person p4 = new Person("Janet", 33);
    		p4.addAccount(new Account("719-100-0019", Account.Type.SAVINGS, 1209.10d));
    		mongoTemplate.insert(p4);
    		
    		assertEquals(4, mongoTemplate.getCollection("MyCollection").count());
    		
    //		WriteResult wr = mongoTemplate.updateMulti(
    //				new Query(where("accounts.accountType").not().is(Account.Type.SAVINGS)),
    //				new Update().inc("accounts.$.balance", 50.00));
    		
    		WriteResult wr = mongoTemplate.updateMulti(
    		new Query(where("accounts.accountType").not().is(Account.Type.SAVINGS)),
    		new Update().push("account", new Account("123", Account.Type.SAVINGS, 12345d)));
    		
    		System.out.println(wr);
    		assertNotNull(wr);
    		assertEquals(3, wr.getN());	
    	}
    this is my line:
    Code:
    WriteResult wr = mongoTemplate.updateMulti(
    		new Query(where("accounts.accountType").not().is(Account.Type.SAVINGS)),
    		new Update().push("account", new Account("123", Account.Type.SAVINGS, 12345d)));
    i use mongo-java-driver 2.5.3, and spring.data.mongo 1.0.0.M2

    thanks for help

  • #2
    a new question on update()

    at last testcase, i want to increase Bob's balance only, so i modified the query from
    Code:
    WriteResult wr = mongoTemplate.updateMulti(
        new Query(where("accounts.accountType").not().is(Account.Type.SAVINGS)),
        new Update().inc("accounts.$.balance", 50.00));
    to
    Code:
    WriteResult wr = mongoTemplate.updateMulti(
        new Query(where("id").is(p1.getId())), ,
        new Update().inc("accounts.$.balance", 50.00));
    but it is not work.
    this is my changed query:
    new Query(where("id").is(p1.getId()))

    thank you for help!

    Comment


    • #3
      Hi,
      Just not to keep you hanging, we will take a look and get back to you ASAP. Thanks so much for the sample app/blog entry!
      Mark

      Comment


      • #4
        the first one (push a pojo) seems a bug of mongo java driver, and i convert my pojo to a DBObject for resolve the problem.
        the second one is not a bug(but not friendly enough), i write the query like this and resolved the problem:
        new Query(where("_id").is(new ObjectId(id)))

        Comment


        • #5
          thanks. solved my "id bug" issue
          whereId() seems work in save, update entire document and find, but not work in updateFirst/multiple

          Comment


          • #6
            Hi,

            This is being tracked now in https://jira.springsource.org/browse/DATADOC-97

            Thanks for reporting it.
            Cheers,
            Mark

            Comment

            Working...
            X