如何使用MongoDB Scala驱动程序设置分片键

时间:2018-01-10 15:18:10

标签: mongodb scala

我想以编程方式为MongoDB数据库启用分片并使用MongoDB Scala驱动程序设置分片键。

namespace Count.Controllers
{

[Route("api/[controller]")]
public class ValuesController : Controller
{
    const string LogFolder = @"C:\Data\Count";
    String[] result = Directory.GetFiles(LogFolder);

    // GET api/values
    [HttpGet]
    //  [AllowJsonGet] Dissalowed in .net core 2.0

    public JsonResult Get()
    {
        List<Count.Models.ReadItems> clicklist = new List<Models.ReadItems>();
        List<string> allFiles = System.IO.Directory.GetFiles(LogFolder).ToList();
        allFiles.Reverse(); //newest on top
        Models.ReadItems temp;
        int i = 0;
        foreach (string File in allFiles)
        {
            temp = new Models.ReadItems();
            temp.filename = File.Replace(LogFolder + @"\\", ""); // remove real path
            temp.url = "../../Api/values/" + i.ToString();  // <==not working
            clicklist.Add(temp );
            i++;
        }
        return Json(clicklist);
}

我想知道我是否可以使用:

val mongoClient: MongoClient = MongoClient("mongodb://localhost")
val db = mongoClient.getDatabase("mydatabase")
val views = db.getCollection("mycollection")
val obs = views.createIndex(ascending("x"))

但我在文档中找不到任何内容。

1 个答案:

答案 0 :(得分:0)

启用分片:

val db = mongoClient.getDatabase("admin")
val obs = db.runCommand(Document("enableSharding" -> "mydatabase")) 

要对集合进行分片:

val db = mongoClient.getDatabase("admin")
val obs = db.runCommand(Document("shardCollection" -> "mydatabase.mycollection", "key" -> Document("xx" -> 1)))