#MongoDB how to insert a document with specified _id
ernestyears opened this issue · 0 comments
Hi. Please kindly help me...
I need to insert a document with a specified _id.
The whole document e.g. with the value "{"_id":{"$oid":"123abc"},"product_id":4,"name":"Kiwi Gold Zespri"}"
and I use following lines of code:
Document document = new Document();
document.append("_id", {"$oid":"123abc"});
document.append("product_id", 4);
document.append("name", "Kiwi Gold Zespri");
connector.saveDocument(document, "myTestDb", "Product");
But this will give the failure "invalid expressions".
Instead, If i use document.append("_id", "123abc"); and it will say
com.lordofthejars.nosqlunit.core.NoSqlAssertionError: Object # Document{{_id=123abc, product_id=4, name=Kiwi Gold Zespri}} # is not found into collection [Product]
at com.lordofthejars.nosqlunit.core.FailureHandler.createFailure(FailureHandler.java:7)
at com.lordofthejars.nosqlunit.mongodb.MongoDbAssertion.checkCollectionObjects(MongoDbAssertion.java:105)
at com.lordofthejars.nosqlunit.mongodb.MongoDbAssertion.strictAssertEquals(MongoDbAssertion.java:40)
at com.lordofthejars.nosqlunit.mongodb.DefaultComparisonStrategy.compare(DefaultComparisonStrategy.java:18)
at com.lordofthejars.nosqlunit.mongodb.DefaultComparisonStrategy.compare(DefaultComparisonStrategy.java:11)
at com.lordofthejars.nosqlunit.core.AbstractCustomizableDatabaseOperation.executeComparison(AbstractCustomizableDatabaseOperation.java:15)
at com.lordofthejars.nosqlunit.mongodb.MongoOperation.compareData(MongoOperation.java:109)
at com.lordofthejars.nosqlunit.mongodb.MongoOperation.databaseIs(MongoOperation.java:102)
at com.lordofthejars.nosqlunit.core.AbstractNoSqlTestRule$1.assertExpectation(AbstractNoSqlTestRule.java:263)
at com.lordofthejars.nosqlunit.core.AbstractNoSqlTestRule$1.evaluate(AbstractNoSqlTestRule.java:83)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Is there any other expressions for the _id?
Thank you in advance.