- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
谁能帮我强调 getCollection(String name) 和 getCollectionFromString(String collectionName) 之间的区别,如 MongoDB DB Java api 中所述?
public DBCollection getCollectionFromString(String collectionName)
Returns a collection matching a given string.
Parameters: collectionName - the name of the collection
Returns: the collection
和
public DBCollection getCollection(String name)
Gets a collection with a given name.
Parameters: name - the name of the collection to return
Returns: the collection
带有示例的插图会很有帮助。(来自 https://api.mongodb.org/java/3.0/ )
最佳答案
Karl Seguin 对此 forum 的回应:
A collection can be identified by a namespace, "db.collection". getCollectionFromString gets the collection for that sort of namespace. getCollection gets it for just the raw collection.
The places where you'd need/use getCollectionFromString have more to do with building some type of mongodb management tool or possibly a CMS which spans multiple databases...that sort of dynamic driven system. getCollection is probably more typical for most apps.
另一个 explanation :
I think which you use will really depend on what you are expecting your users to enter.
If they don't know anything about all the databases, then using getCollection() makes sense...which I'm thinking would be the case.
If it's a truly dynamic system and the user might say "get me the users from the database app1" then the getCollectionFromStirng might make sense.
However, I'd argue that int the 2nd case, it would be better to simply have two fields: Database and Collection. That way it's more explicit and simpler to deal with. Why should your users know that database and collection are dot separated in MongoDB?
一个 example 来说明getCollection(String s)
:
package com.example.core;
import java.net.UnknownHostException;
import java.util.Set;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
* Java : Get collection from MongoDB
*
*/
public class GetCollectionApp {
public static void main(String[] args) {
try {
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("yourdb");
// get list of collections
Set<String> collections = db.getCollectionNames();
for (String collectionName : collections) {
System.out.println(collectionName);
}
// get a single collection
DBCollection collection = db.getCollection("yourCollection");
System.out.println(collection.toString());
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
Implementation 的 getCollectionFromString(String s)
public DBCollection getCollectionFromString(String s){
DBCollection foo = null;
while (s.contains(".")){
int idx = s.indexOf(".");
String b = s.substring(0, idx);
s = s.substring(idx + 1);
foo = getCollection(b);
}
if (foo != null)
return foo;
return getCollection(s);
}
关于java - MongoDB - getCollection(String name) 和 getCollectionFromString(String collectionName) 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29085102/
下面的代码运行流畅。我不想完成任何具体的事情。然而我是;确信 collectionName 参数必须有更多可能的值。有人知道可能值的完整列表吗? void Foo(string pathToAcces
我正在查看 db.system.profile.stats() 的输出,我很好奇 max 字段在返回文档中的含义(运行mongodb 2.2.2)。 这是一个例子: > db.system.profi
我想检查 Node.js 中是否存在一个集合。我使用 db.collectionNames 获取数据库中的名称列表,但没有任何反应。代码: connectDB(DBURL).then(function
这个问题已经有答案了: 'process.nextTick(function() { throw err; })' - Undefined is not a function (mongodb/mon
谁能帮我强调 getCollection(String name) 和 getCollectionFromString(String collectionName) 之间的区别,如 MongoDB D
当我在 Visual Studio 2012 Professional 中打开现有解决方案时,出现上述错误。这曾经可以工作,但在重建计算机后出现了问题。 我已经安装了 Visual Studio 20
我是一名优秀的程序员,十分优秀!