如何使用java驱动程序或morphia获取保存在mongodb中的模型的id?
有一个教程
http://code.google.com/ docreader/#p=morphia&s=morphia&t=QuickStart
但它省略了如何获取酒店 id 的细节。
Morphia morphia = ...;
Datastore ds = morphia.createDatastore("testDB");
String hotelId = ...; // the ID of the hotel we want to load
// and then map it to our Hotel object
Hotel hotel = ds.get(Hotel.class, hotelId);
我认为它要么是 mongo 的 java 驱动程序,要么是 morphia API。 那么如何获取id呢?
There is a tutorial
http://code.google.com/docreader/#p=morphia&s=morphia&t=QuickStart
But it leaves out the detail how to get the id of the hotel.
Morphia morphia = ...;
Datastore ds = morphia.createDatastore("testDB");
String hotelId = ...; // the ID of the hotel we want to load
// and then map it to our Hotel object
Hotel hotel = ds.get(Hotel.class, hotelId);
I thinks its either java driver for mongo or morphia API.
So how do I get the id?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与您使用 RDBMS 时的做法没有什么不同。
假设您正在构建一个旅游网站。现在您有了一个数据库,其中已经包含与其所属城市关联的酒店。
现在,我作为用户进来并请求显示纽约的所有酒店,然后您将转到数据库并查询纽约的所有酒店,然后从您的应用程序发回一个列表,可能会呈现进入页面,然后将结果显示给用户。这个结果页面至少会有数据库中的 ID 和酒店名称。现在,一旦用户想要查看更多详细信息(例如华尔道夫酒店),他们就会单击该详细信息,然后该酒店的“我”将传回您的应用程序。现在,您拥有的代码示例可用于加载酒店的所有详细信息,然后将它们显示给用户。
有道理吗?
总而言之,要加载的记录的 Id 通常来自用户输入,您加载 Ida 列表,然后用户可以选择一个或多个。要获取 Ida 的列表,您通常可以通过一些参数进行查询,在上述情况下,通过城市名称进行查询。
现在,要继续使用上面的示例,您可以将 id 硬编码为数据库中已有的值并完成代码。
另一种选择是,您可以创建酒店对象的一个新实例,然后像文档中所说的那样发出 ave 命令,然后一旦您这样做,如果您有一个映射为 ID 的属性,您可以对该属性执行 get 操作,然后将其传递给我的注释,然后执行读取查询。
This is no different from how you would do it, say you were using a RDBMS.
Let's say you are building a travel site. Now you have a database which already has the hotels that are associated with the cities that they belong to.
Now I as a user come in and request to be shown all the hotels, in, new York, then you would go to the database and query for all hotels that are in new York and then send back a list from your application, maybe rendered into a page and then display the results to the user. This results page, will at least have, the id thats in the database, and the hotel name. Now, once the user wants to see more details, say of The Waldorf hotel, they would click on that, and the I'd for that hotel would be passed back to your application. Now the code sample you have can be then used to load all the details of the hotel a Nd then display them to the user.
Make sense?
To summarize, the I'd of the record you want to load, typically comes from user input, where you load a list of Ida and the user can then select one or more. And to get the list of Ida, you typically query by some parameters, in the above case, by the city name.
Now, to proceed with using the sample above, you can hard code the id, to a value that you already have in the db and complete the code.
Another option, you can create a new instance of the hotel object and then issue the ave command like the docs say and then once you do that, if you have a property mapped as an ID, you can do a get on that property and then pass it along to the the note I'd, and then execute the read query.