Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从mongoShell更新mongodb文档后无法使用domain.get()_Mongodb_Grails_Groovy_Morphia - Fatal编程技术网

从mongoShell更新mongodb文档后无法使用domain.get()

从mongoShell更新mongodb文档后无法使用domain.get(),mongodb,grails,groovy,morphia,Mongodb,Grails,Groovy,Morphia,我最近开始使用grails,它非常棒。在我的应用程序中,我使用morphia插件与mongodb进行通信。让我给你一个系统的概述,然后进入我所面临的问题 概述 我有三个域类,如下所示 A. Project.groovy B. Customer.groovy C. User.groovy Project.groovy具有以下特性 @Reference Customer customer String projectName @Id ObjectId projectId @Reference L

我最近开始使用grails,它非常棒。在我的应用程序中,我使用morphia插件与mongodb进行通信。让我给你一个系统的概述,然后进入我所面临的问题

概述 我有三个域类,如下所示

A. Project.groovy
B. Customer.groovy
C. User.groovy
Project.groovy具有以下特性

@Reference Customer customer
String projectName
@Id ObjectId projectId
@Reference List<Project> projects
String customerName
@Id ObjectId customerId
@Reference List<Project> userResponsibleProjects
String userName
@Id String userEmail
db.User.update({_id: userA},{$pull: {$ref: "Project", $id: ObjectId("projectB")}})
Customer.groovy具有以下特性

@Reference Customer customer
String projectName
@Id ObjectId projectId
@Reference List<Project> projects
String customerName
@Id ObjectId customerId
@Reference List<Project> userResponsibleProjects
String userName
@Id String userEmail
db.User.update({_id: userA},{$pull: {$ref: "Project", $id: ObjectId("projectB")}})
关于这个问题的一些信息,以及这个问题是如何产生的。系统外的用户可以选择删除他们不再使用的项目。因此,当触发删除项目时,我将删除对该项目的引用并保存客户,如果客户保存成功,我将删除该项目。因为我是新手,所以我没有在User.groovy中检查项目引用。假设我们删除了projectB,我删除了

customer.remove(projectB)
if(customer.save())
    projectB.remove()
现在我面临的最初问题是

“grails异常无法获取user.userResponsibleProjects的项目引用”。所以为了解决这个问题,我进入数据库,做了以下工作

@Reference Customer customer
String projectName
@Id ObjectId projectId
@Reference List<Project> projects
String customerName
@Id ObjectId customerId
@Reference List<Project> userResponsibleProjects
String userName
@Id String userEmail
db.User.update({_id: userA},{$pull: {$ref: "Project", $id: ObjectId("projectB")}})
这起作用了。现在才是真正的问题

如果用户试图进入任何客户,系统会抛出相同的异常,即无法获取user.userResponsibleProjects的参考。在进一步调查中,我发现以下情况,我使用

Customer customer = Customer.get(params.customerId)
即使mongodb中存在具有相同id的客户文档,也会失败

但如果我做了这样的事情

Customer customer = Customer.list().toList().find { it.id.toString() == params.customerId}
它起作用了

知道为什么会这样吗?我不确定这是否是mongodb或morphia的问题。 非常感谢您的帮助


提前感谢:)

经过几个小时的调试,我发现了问题:),问题是mongo中存在对已删除文档的交叉引用,如果您有任何对象的副本(该副本引用了mongo中的其他文档),通常会发生这种情况。为避免此问题,请确保没有副本,如果有,请确保它们是嵌入的或引用的副本。

我没有使用Morphia/Grails,但我使用过Grails和Mongo(Gorm)…您的字符串/对象ID不匹配。Customer.get()接受字符串还是ObjectId?它接受ObjectId,但当您使用domain.get()时,这并不重要,最奇怪的是,同样的代码适用于CustomerA。这很奇怪,如果这是您所说的,即字符串/对象不匹配,那么它不应该适用于任何其他客户对象。。但这是真的!看看我的另一篇关于如何纠正这个问题的帖子