我有一个用Play和ReactiveMongo编写的应用程序,我想要:
landingPage
文档插入MongoDB的操作.我有这个工作代码:
// Insert the landing page and wait for it to be inserted, so we can then get the new count of landing pages.
val futures = for {
wr <- landingPagesCollection.insert(landingPage)
count <- landingPagesCollection.count()
} yield count
futures.map { (count: Int) =>
Created(Json.obj(
"created" -> true,
"landingPage" -> landingPage.toJson,
"count" -> count
))
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常.但是,出于好奇,我想知道如何访问wr
(WriteResult)值.当我将代码更改为:
val futures = for {
wr <- landingPagesCollection.insert(landingPage)
count <- landingPagesCollection.count()
} yield (wr, count)
futures.map …
Run Code Online (Sandbox Code Playgroud) 要创建List,为什么Java不允许创建它们然后逐个添加元素?
这有效:
public static List<TrackedItem> create(List<Item> items)
{
TrackedItem[] arr = new TrackedItem[items.size()];
int i = 0;
for (Item item : items)
{
arr[i] = TrackedItem.createOrUpdate(item);
i++;
}
return java.util.Arrays.asList(arr);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用(tracked.add()
导致a NullPointerException
):
public static List<TrackedItem> create(List<Item> items)
{
List<TrackedItem> tracked = java.util.Collections.emptyList();
for (Item item : items)
{
tracked.add(TrackedItem.createOrUpdate(item));
}
return tracked;
}
Run Code Online (Sandbox Code Playgroud) 我试图使这需要的参数的方法Country.class
,User.class
等等,并返回argument.count()
.
我将为此方法提供的所有可能的类都扩展Model
并拥有该方法count()
.
我的代码:
private static long <T> countModel(Model<T> clazz)
{
// there is other important stuff here, which prevents me from
// simply by-passing the method altogether.
return clazz.count();
}
Run Code Online (Sandbox Code Playgroud)
被称为:
renderArgs.put("countryCount", countModel(Country.class));
Run Code Online (Sandbox Code Playgroud)
然而,这根本不起作用.
我该怎么办?
其他人问过这个,但答案都是针对jQuery的.
java ×2
casting ×1
future ×1
generics ×1
html5 ×1
javascript ×1
mongodb ×1
mootools ×1
placeholder ×1
scala ×1