MongoDB 在 Java 中创建嵌入式对象,无需吗啡。

发布于 2024-11-26 17:12:36 字数 1241 浏览 2 评论 0原文

我对 mongodb 非常陌生,并尝试使用它进行开发。 我有一个概念模型:
User={"uid":"", "服务":"[
<代码>{
"serviceid":"sid",
"sub_dat":"某个日期",
"exp_date":"某个日期",
<代码>},
<代码>{
"serviceid":"sid",
"sub_dat":"某个日期",
"exp_date":"某个日期",
<代码>},
<代码>{ "serviceid":"sid",
"sub_dat":"某个日期",
"exp_date":"某个日期",
<代码>},
]”,
“朋友”:“[
<代码>{
"friend_id":"",
"朋友姓名":"姓名"
"friendshipyrs":"年",
<代码>},

<代码>{
"friend_id":"",
"朋友姓名":"姓名"
"friendshipyrs":"年"
},

]", }

我想知道在 java 中使用原始驱动程序要遵循的步骤,而不是吗啡 to, to : 1.创建这个对象,这样我就有能力: 2. 获取新服务和好友并将其追加到服务列表中。

我目前可以添加到顶层,并且尝试使用 BasicDBObject、DBList 甚至 ObjectBUilder,但无法弄清楚如何附加或推入字段以使用 java 驱动程序创建数组/列表,如可以从10gen 网站上的演示。

另外,希望能够通过单个查询深入了解..朋友信息,因此建议保留上述结构还是将朋友创建为类并将类对象放入列表中?我知道点运算符,但我不知道如何通过 java 驱动程序访问类字段。

如果有任何帮助,我们将非常感激... 谢谢

I am very new to mongodb and tryn to use it for developement.
I have a conceptual model of:

User={"uid":"", "services":"[

{

"serviceid":"sid",
"sub_dat":"somedate",
"exp_date":"somedate",
},

{

"serviceid":"sid",
"sub_dat":"somedate",
"exp_date":"somedate",
},
{
"serviceid":"sid",
"sub_dat":"somedate",
"exp_date":"somedate",
},
]",

"friends":"[

{

"friend_id":"",

"friendname":"name"

"friendshipyrs":"yrs",

},

{

"friend_id":"",

"friendname":"name"

"friendshipyrs":"yrs"

},

]", }

I wish to know the steps to follow in java with the raw driver, not morphia to, to:
1. create this object, such that i have the ability to:
2. fetch and append new services and friends to the service lists.

I can currently add to the top level and I tried using the BasicDBObject, DBList, and even the ObjectBUilder but could not figure out how to append or push into the fields to create the arrays/lists with the java driver as can be seen from the presentations on the 10gen site.

Also, want to be able to drill down to say.. friend info with a single query, so will it be advisable maintain above structure or create friends as a class and put the class objects in the list? I know of the dot operator, but i do not know how to access class fields through the java driver.

Will be very greatful for any help...
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半边脸i 2024-12-03 17:12:36

首先,每篇文章问一个问题会更容易;)

使用 java 驱动程序进行更新非常简单,虽然有点冗长:

假设你的 mongo 更新看起来像:

db.users.friends({_id: <someuserid>}, {$push:{friends:{friend_id:...., friendname: ....}}})

在 Java 中你所要做的就是创建一个封装该内容的 DBObject更新,所以在这种情况下:

DBObject query = new BasicDBObject("_id", <someuserid>);

DBObject newFriend = new BasicDBObject("friend_id", ...);
newFriend.put("friendname", ....);

DBObject update = new BasicDBObject("$push", new BasicDBObject("friends", newFriend));

DBCollection col = db.getCollection("users");

col.update(query, update);

请原谅任何拼写错误,我实际上没有对这个或任何内容进行语法检查,但它应该可以帮助您开始。值得注意的是,在尝试使用 Java 之前,能够在 shell 中执行这些操作非常重要。 API 还具有 QueryBuilder,可以简化查询对象的构造。

完整的 API 文档可以在这里找到:http://api.mongodb.org/java/2.6。 3/

First of all it's easier to ask one question per post ;)

Doing updates with the java driver is pretty straightforward, if a bit verbose :

Say your mongo update would look like :

db.users.friends({_id: <someuserid>}, {$push:{friends:{friend_id:...., friendname: ....}}})

All you have to do in Java is create a DBObject that encapsulates that update, so in this case :

DBObject query = new BasicDBObject("_id", <someuserid>);

DBObject newFriend = new BasicDBObject("friend_id", ...);
newFriend.put("friendname", ....);

DBObject update = new BasicDBObject("$push", new BasicDBObject("friends", newFriend));

DBCollection col = db.getCollection("users");

col.update(query, update);

Excuse any typoes, I haven't actually syntax checked this or anything but it should get you started. It's worth noting that it's important to be able to do these things in shell before trying it in Java. The API als has QueryBuilder which eases query object construction.

Full API documentation can be found here : http://api.mongodb.org/java/2.6.3/

等数载,海棠开 2024-12-03 17:12:36

就像 Remon 所说的那样,我认为使用 ORM 是个好主意,因为它通常提供比您自己的方法更适合生产的方法。听起来你想对我使用对象引用。通过使用 @Reference 注释,我能够将对象“嵌入”到另一个对象中,然后一旦加载了拥有的对象,我就可以访问拥有的对象(我认为这使用了预加载)。举个例子:

@Entity public class PlaylistItem extends SomeModel{
   @Required
   @Reference
   public Playlist playlist;

   @Required
   @Reference
   public Track track;
}

Playlist和Track是两个用@Entity注释的模型,就像这个PlaylistItem对象一样。

希望这有帮助。

有关 Morphia 注释的更多信息,请访问:http://code.google.com/p/morphia/ wiki/AllAnnotations。甚至还有一个 Embeddd 注释,但我从来没有使用过它。

Like Remon said, I think it's a good idea to go with an ORM as it usually presents a more production-ready approach than rolling your own. It sounds like you want to use object references to me. By using the @Reference annotation, I am able to "embed" objects within another object and then once I've loaded the owning object, I can get access to the owned object (I think this uses eager loading). An example:

@Entity public class PlaylistItem extends SomeModel{
   @Required
   @Reference
   public Playlist playlist;

   @Required
   @Reference
   public Track track;
}

Playlist and Track are two models annotated with @Entity, just like this PlaylistItem object.

Hope this helps.

More info on Morphia annotations here: http://code.google.com/p/morphia/wiki/AllAnnotations. There is even an Embeddd annotation but I have never had to use that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文