UpdateOperations.add() to an array field that doesn't exist yet

355 views
Skip to first unread message

tony_k

unread,
Jul 6, 2011, 3:33:04 PM7/6/11
to Morphia
newbie morphia user having what is probably a stupid issue:

have a three-level object graph Widgit has a list of Whatsits has a
list of Whoosits.

so i create a Widget with an empty list of Whatsits to start with.

then i attempt:

---
// repo is a BasicDAO<Widgit, ObjectId>...
//
UpdateOperations<Widgit> ops =
repo.createUpdateOperations().add("whatsits.whoosits", new Whoosit());
---

i get: MongoException: can't append to array using string field name
[whoosits]

i was hoping morphia/mongo would create the "whoosits" field as an
array since it didn't exist, but no dice.

any guidance appreciated...!

Scott Hernandez

unread,
Jul 6, 2011, 4:03:04 PM7/6/11
to mor...@googlegroups.com
Can you provide example classes or an example document (json) with
what you are trying to do? It is hard to update things deep in nested
arrays of arrays.

tony_k

unread,
Jul 6, 2011, 8:19:23 PM7/6/11
to Morphia
@Entity
public class Widgit
{
@Id
private ObjectId id;

private List<Whatsit> whatsits = new ArrayList<Whatsit>();

public ObjectId getId()
{
return id;
}

public List<Whatsit> getWhatsits()
{
return whatsits;
}

public void setWhatsits(List<Whatsit> whatsits)
{
this.whatsits = whatsits;

tony_k

unread,
Jul 6, 2011, 8:27:23 PM7/6/11
to Morphia
oops, sorry about the earlier mis-post:


@Entity
public class Widgit
{
@Id
private ObjectId id;

private List<Whatsit> whatsits = new ArrayList<Whatsit>();

public ObjectId getId()
{
return id;
}

public List<Whatsit> getWhatsits()
{
return whatsits;
}

public void setWhatsits(List<Whatsit> whatsits)
{
this.whatsits = whatsits;
}
}

public class Whatsit
{
private Integer id;

private List<Whoosit> whoosits = new ArrayList<Whoosit>();

protected Whatsit()
{
}

public Whatsit(Integer id)
{
this.id = id;
}

public Integer getId()
{
return id;
}

public List<Whoosit> getWhoosits()
{
return whoosits;
}

public void setWhoosits(List<Whoosit> whoosits)
{
this.whoosits = whoosits;
}
}


public class Whoosit
{
private Integer id;

protected Whoosit()
{
}

public Whoosit(Integer id)
{
this.id = id;
}

public Integer getId()
{
return id;
}
}

//... repo is a BasicDAO...
//
Widgit widgit = new Widgit();
widgit.getWhatsits().add(new Whatsit(0));
repo.save(widgit);
Query<Widgit> q =
repo.createQuery().field(Mapper.ID_KEY).equal(widgit.getId()).field("whatsits.id").equal(0);
Whoosit whoosit = new Whoosit(1);
UpdateOperations<Widgit> ops =
repo.createUpdateOperations().add("whatsits.whoosits", whoosit);
UpdateResults<Widgit> results = repo.update(q, ops);
//...

> db.Widgit.find()
{ "_id" : ObjectId("4e14faddc0f7a9dfecd662d0"), "className" :
"com.kerz.morphia.Widgit", "whatsits" : [ { "id" : 0 } ] }

On Jul 6, 4:03 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:

tony_k

unread,
Jul 6, 2011, 8:29:09 PM7/6/11
to Morphia
and the trace if it's helpful:

com.mongodb.MongoException: can't append to array using string field
name [whoosits]
at com.mongodb.CommandResult.getException(CommandResult.java:82)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:116)
at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:
125)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:147)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:131)
at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:335)
at com.google.code.morphia.DatastoreImpl.update(DatastoreImpl.java:
950)
at com.google.code.morphia.DatastoreImpl.update(DatastoreImpl.java:
916)
at com.google.code.morphia.DatastoreImpl.update(DatastoreImpl.java:
921)
at com.google.code.morphia.DatastoreImpl.update(DatastoreImpl.java:
837)
at com.google.code.morphia.dao.BasicDAO.update(BasicDAO.java:129)
at com.kerz.morphia.MorphiaTest.basic(MorphiaTest.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod
$1.runReflectiveCall(FrameworkMethod.java:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:
15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:
41)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:
20)
at
org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:
74)
at
org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:
82)
at
org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:
72)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:
240)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at
org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:
61)
at
org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:
70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:
180)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
49)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
197)
> repo.createQuery().field(Mapper.ID_KEY).equal(widgit.getId()).field("whatsi ts.id").equal(0);
Reply all
Reply to author
Forward
0 new messages