gpt4 book ai didi

java - MongoDB - getCollection(String name) 和 getCollectionFromString(String collectionName) 之间的区别?

转载 作者:可可西里 更新时间:2023-11-01 10:43:16 26 4
gpt4 key购买 nike

谁能帮我强调 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com