如何让Morphia忽略多余的field/element?

How to Morphia ignore extra field/element?

我是新手,当我使用吗啡时 java mongodb 我遇到了问题。 我在 mongodb 中有 "news" 数据库,集合是 "article" 示例文章:

{
   "_id": ObjectId("54b77156d52fbb115abbc7ed"),
   "title": "test title",
   "thumbnail": "http://test.com/images/2015/01/15/kt169333-1.jpg",
   "imgsLoc": "/home/test/public/images/2015/01/15/kt169333-1.jpg",
   "sapo": "SampleSapo",
   "author": null,
   "content": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
   "source": "http://example.com/news/test/1",
 ... [it has more about 30 fields after this line ...]
}

我的工作只需要获取内容并保存新文章的textLength字段。 像这样输出:

{
   "_id": ObjectId("54b77156d52fbb115abbc7ed"),
   "title": "test title",
   "thumbnail": "http://test.com/images/2015/01/15/kt169333-1.jpg",
   "imgsLoc": "/home/test/public/images/2015/01/15/kt169333-1.jpg",
   "sapo": "SampleSapo",
   "author": null,
   "content": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"textLength": NumberLong(441) <= new field
   "source": "http://example.com/news/test/1",
 ... [it has more about 30 fields after this line ...]
}

当我使用吗啡时 因为我只需要得到 content 来计算 textlength 所以我不关心其他领域。 但是当我使用吗啡时。其他字段丢失,只有 content 和 textLength 字段保存在数据库中; 我的文章class要映射的是:

public class Article {
@Id
protected ObjectId id;
protected String content;
protected long textLength;

// Getset method

}

为什么其他字段丢失了?

在不丢失数据的情况下更新集合的最快方法是什么? 将本文 class 映射到 mongodb 时如何忽略额外字段?

谢谢

即使您没有在代码中显示它,我也假设您在文档上使用了保存。因此,您将其替换为您拥有的属性,因此 "missing" 字段。

如果您不想映射整个文档,则需要显式更新字段而不是简单地调用保存。见 Morphia documentation on updates.