The structure of document in mongo is as follows
Project(@Entity)
|--Deliverables(@Embedded, list inside Project)
|--DeliveryTypes(@Embedded, list inside Deliverables)
|--DeliveryItems(Plain list inside DeliveryTypes)
|--log(Plain list inside DeliveryItems)
and the other possible structure of document in mongo is as follows
Project(@Entity)
|--Deliverables(@Embedded, list inside Project)
|--DeliveryTypes(@Embedded, list inside Deliverables)
|--DeliveryItems(Plain list inside DeliveryTypes)
|--Tasks(Plain list inside DeliveryItems)
|--log(Plain list inside Tasks)
|--log(Plain list inside DeliveryItems)
This is how the Project document looks like in mongoDB
{
"_id" : ObjectId("51827f4fe4b07cc5088149ff"),
"className" : "Project",
"name" : "TestProject",
"description" : "This is a test project",
"state" : "Open",
"dateCreated" : ISODate("2013-05-02T14:59:27.069Z"),
"projectStatuses" : [
{
"status" : "On Track",
"dateCreated" : ISODate("2013-05-02T14:59:27.071Z"),
"user" : {
"_id" : "[email protected]",
"firstName" : "Test",
"lastName" : "User",
"enabled" : true,
"accountExpired" : false,
"accountLocked" : false,
"passwordExpired" : false
}
}
],
"commercialStatuses" : [
{
"status" : "On Track",
"dateCreated" : ISODate("2013-05-02T14:59:27.074Z"),
"user" : {
"_id" : "[email protected]",
"firstName" : "Test",
"lastName" : "User",
"enabled" : true,
"accountExpired" : false,
"accountLocked" : false,
"passwordExpired" : false
}
}
],
"deliverables" : [
{
"embeddedId" : ObjectId("5183702fe4b014bfbe387d37"),
"name" : "TestSite 01",
"deliveryTypes" : [
{
"deliveryItems" : [
{
"embeddedId" : ObjectId("5183702fe4b014bf00000003"),
"type" : "Plain",
"log" : [
{
"dateCreated" : ISODate("2013-05-03T08:42:10.592Z"),
"oldValue" : "Open",
"newValue" : "Closed",
"user" : {
"_id" : "[email protected]",
"firstName" : "Test",
"lastName" : "User",
"enabled" : true,
"accountExpired" : false,
"accountLocked" : false,
"passwordExpired" : false
}
},
{
"dateCreated" : ISODate("2013-05-03T09:24:30.336Z"),
"oldValue" : "Closed",
"newValue" : "Open",
"user" : {
"_id" : "[email protected]",
"firstName" : "Test",
"lastName" : "User",
"enabled" : true,
"accountExpired" : false,
"accountLocked" : false,
"passwordExpired" : false
}
},
{
"dateCreated" : ISODate("2013-05-03T13:33:06.550Z"),
"oldValue" : "Open",
"newValue" : "Closed",
"user" : {
"_id" : "[email protected]",
"firstName" : "Test",
"lastName" : "User",
"enabled" : true,
"accountExpired" : false,
"accountLocked" : false,
"passwordExpired" : false
}
}
]
},
{
"embeddedId" : ObjectId("5183702fe4b014bf00000004"),
"type" : "task",
"tasks" : [
{
"embeddedId" : ObjectId("518370abe4b014bf00000001"),
"name" : "TestSubTask 1",
"log" : [
{
"dateCreated" : ISODate("2013-05-03T08:09:15.624Z"),
"oldValue" : "Open",
"newValue" : "Created",
"user" : {
"_id" : "[email protected]",
"firstName" : "Test",
"lastName" : "User",
"enabled" : true,
"accountExpired" : false,
"accountLocked" : false,
"passwordExpired" : false
}
}
],
"plannedEndDate" : ISODate("2013-05-02T22:00:00Z"),
"assignedUser" : "Test person",
"description" : "This is a test sub task"
}
],
"log" : [
{
"dateCreated" : ISODate("2013-05-03T08:07:52.725Z"),
"oldValue" : "Open",
"newValue" : "Closed",
"user" : {
"_id" : "[email protected]",
"firstName" : "Test",
"lastName" : "User",
"enabled" : true,
"accountExpired" : false,
"accountLocked" : false,
"passwordExpired" : false
}
}
]
}
]
}
]
}
now the problem I have, I want to change all the occurrences of
"user" : {
"_id" : "[email protected]",
"firstName" : "Test",
"lastName" : "User",
"enabled" : true,
"accountExpired" : false,"accountLocked" : false,
"passwordExpired" : false
}
under
projectStatuses, commercialStatuses, and log
to
"user" : DBRef("User", "[email protected]")
This is what I have tried so far,
Mongo connection = new Mongo("localhost", 27017)
DB db = connection.getDB('test')
DBCollection projectCollection = Project.collection
QueryBuilder projectQuery = new QueryBuilder()
BasicDBObject projectKeys = new BasicDBObject()
DBCursor projectCursor = projectCollection.find(projectQuery.get(), projectKeys)
ArrayList projects = projectCursor.toArray()
projects.each { project ->
project.deliverables.each { deliverable ->
deliverable.deliveryTypes.each { deliveryType ->
deliveryType.deliveryItems.each { deliveryItem ->
deliveryItem.log.each { log ->
updateLogUser(log, db)
}
}
}
}
}
static updateLogUser(def log, DB db) {
if (log.user?._id) {
log.user = new DBRef(db, "User", log.user?._id)
}
}
The above code does what I need, but the problem now is how do I save the updated, queried objects? I tried the following but grails keep throwing exception "cannnot cast BasicDBObject to DBRef" when I try to login :/
projects.each { project ->
Project.update(['_id': project._id]) {set "deliverables", project.deliverables}
}
I have checked the db after migration and there is no instance of user object, there are only references, but still get the exception. I am not really good with writing migrating scripts, so if someone can help me in how to save queried documents (which looks like a big map), it would be great, or some useful links to how-to documents would also be helpful. Thanks in advance :)
See Question&Answers more detail:
os