我一直试图开始尝试使用C#官方驱动程序创建和查询MongoDB,然后遇到同样的岩石.问题是如何使用地理信息创建数据.我只是没有找到答案.
码:
MongoUrl url = new MongoUrl("mongodb://xxx.xx.x.xx/mydb");
MongoServer server = MongoServer.Create(url);
MongoDatabase database = server.GetDatabase("mydb");
Run Code Online (Sandbox Code Playgroud)
< - 这很好用
BsonDocument[] batch = {
new BsonDocument {
{ "name", "Bran" },
{ "loc", "10, 10" }
},
new BsonDocument {
{ "name", "Ayla" },
{ "loc", "0, 0" }
}
};
places.InsertBatch(batch);
Run Code Online (Sandbox Code Playgroud)
< - 那部分是错误的
places.EnsureIndex(IndexKeys.GeoSpatial("loca"));
var queryplaces = Query.WithinCircle("loca", 0, 0, 11);
var cursor = places.Find(queryplaces);
foreach (var hit in cursor)
{
foreach (var VARIABLE in hit)
{
Console.WriteLine(VARIABLE.Value);
}
} …Run Code Online (Sandbox Code Playgroud) 这就是我过去在实体框架(POCO)中使用继承的方式:
ctx.Animals // base class instances (all instances)
ctx.Animals.OfType<Cat> // inherited class Cat's instances only
ctx.Animals.OfType<Dog> // inherited class Dog's instances only
Run Code Online (Sandbox Code Playgroud)
这是我在MongoDb(MongoDb参考)中找到的唯一类似方式:
var query = Query.EQ("_t", "Cat");
var cursor = collection.FindAs<Animal>(query);
Run Code Online (Sandbox Code Playgroud)
注意在后一种情况下,我必须处理鉴别器("_ t")并硬编码我的类名,这不太方便,看起来很糟糕.如果我错过了查询,我在枚举尝试中得到了一个例外.我错过了什么吗?我的建议是文件Db,它存储对象'应该'容易处理继承.
polymorphism mongodb c#-4.0 mongodb-query mongodb-.net-driver
背景:
我需要完成的是,如果记录中的特定数组为null或为空,则删除集合中的所有记录.
据我所知,查找空数组的C#Driver查询是:
IMongoQuery query = Query.Exists("myArray", false);
Run Code Online (Sandbox Code Playgroud)
这对于检测空数组很好,但有时数组不会为null,但不会有任何元素.我需要的更像是:
// Note: second subquery will not work
IMongoQuery query = Query.Or(
Query.Exists("myArray", false),
Query.IsEmpty("myArray", false) // error
);
Run Code Online (Sandbox Code Playgroud)
模型:
我的班级看起来像:
public class MyClass
{
// This property may be null or empty
[BsonElement("myArray")]
public string[] MyArray { get; set; }
[BsonElement("someElement")]
public int SomeElement{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
摘要:
任何有关这方面的帮助将不胜感激!:)
一些背景:
我通过mongo-elasticsearch河将MongoDB与ElasticSearch结合使用.在Elasticsearch中,我希望我的文档结构如下所示:
{
"_id": "SomeId-AnotherId",
... // all the other lovely denormalized data
}
Run Code Online (Sandbox Code Playgroud)
SomeId-AnotherId是我在对数据进行非规范化时创建的.我需要这种结构的原因是我需要能够说http://elasticsearch/index/type/SomeId-AnotherId要检索文档.
我对我的数据进行了非规范化(一个C#应用程序),然后我插入MongoDB(这些数据然后通过河流进入ES,如上所述).当我插入MongoDB时,我目前的印象是我需要在我的模型上设置一个BsonId,Mongo用它来索引文档.这可以是ObjectId或任何其他类型,如string或int等,只要我添加[BsonId]属性.
我的模型看起来像这样:
public class Model {
[BsonId]
public string Id {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我这样设置:
model.Id = string.format("{0}-{1}", someId, anotherId);
Run Code Online (Sandbox Code Playgroud)
问题
目前我看到大约1,500个文件从〜10,000的插入进入Mongo.我看了一下我为模型对象生成的id,肯定有超过12个字节.mongo会拒绝那些而不是写它们吗?
Bson Id是12字节 - 这是否意味着如果我创建自己的ID(格式为:"SomeId-AnotherId"),它也应该只有12个字节长?无论如何围绕这个?
我不想对这些文档使用mongos默认的objectId,因为如上所述,一旦doc在elasticsearch中,我希望能够以特定的方式获取文档(在URI中使用"SomeId-AnotherId").
最后的笔记:
我知道我可以在我的模型中添加另一个ID属性,例如ElasticId,然后配置Elasticsearch来查找此属性并将其用作elasticsearch文档的_id.如果我这样做,那么我可以使用Mongos默认ID,一切都会好的.但是,我会牺牲弹性搜索性能,我还需要在弹性搜索中存储一个我不想要的额外字段.
对不起大规模的脑转储顺便说一下!! :)
我需要使用如下标准查询来查询MongoDB:
{"$and":[{"Name":"Accelero JKT M Tun XL "}]}
Run Code Online (Sandbox Code Playgroud)
我通常使用C#中的Query对象构建查询,然后执行以下操作:
MongoCollection<BsonDocument> col = _database.GetCollection<BsonDocument>("SourceItem");
var query = Query.And(Query.EQ("AccountID", BsonValue.Create(Convert.ToInt32(_accountID))), Query.EQ("SKU", sku));
var docs = col.Find(query);
Run Code Online (Sandbox Code Playgroud)
由于我已经有了查询,因此我不想使用Query对象来构建查询.我如何简单地使用我已有的查询?
我正在尝试使用MongoDB C#驱动程序将项目添加到BSON文档内的嵌套数组中。我已经搜索过SO,但是到目前为止我发现的所有示例都与我尝试做的不匹配。
我在蒙哥有一个顶级的“组织”收藏。它包含多个具有以下结构的组织对象:
{
"id":"Org1",
"Name":"First Org",
"Divisions":[
{
"id":"Div1",
"Name":"First Division",
"UsersInDivision":[
"User1",
"User2"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我有以下POCO课程
public class Organization
{
public string Id { get; set; }
public string Name { get; set; }
public IList<Division> Divisions { get; set; }
}
public class Division
{
public string Id { get; set; }
public string Name { get; set; }
public IList<string> UsersInDivision { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想将字符串“ User3”添加到“ Div1”或组织“ Org1”的UsersInDivision集合中。实现此目标的最佳方法是什么?我正在尝试尽可能使用MongoDB数据访问类的强类型版本。
我有一个2层的C#项目.第一个是连接到mongodb并将集合发送到Web服务层的数据层.问题是我在新的驱动程序中找不到非async方法(即同步).
有没有办法在mongodb版本2.0中使用C#驱动程序的同步方法?
提示:是否可以使用c#运行mongodb shell函数?
我是MongoDB的新手,我在Web Api中使用它来为移动应用程序提供服务.
现在,我需要运行一个聚合,因为我正在使用C#,我想通过使用Aggregate一个返回我的集合上的命令来流利地完成它IAggregateFluent.
但是,我被困住了,我在SO上找到的信息对我没有帮助,因此是一个新问题.
我已经建立了一个小型集合,其中包含具有一些基本属性的智能手机,智能手机集合中的单个项目看起来像:
{
"name" : "LG Nexus 5",
"description" : "A Nexus 5 device, created by Google.",
"typenr" : "LG-NEX-5/WHITE",
"props" : [
{
"type" : "os",
"value" : "Android"
},
{
"type" : "storage",
"value" : "8"
},
{
"type" : "storage",
"value" : "16"
},
{
"type" : "storage",
"value" : "32"
},
{
"type" : "storage",
"value" : "64"
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在,我在shell中创建了一个聚合,如下所示:
// Get all the amount of filters …Run Code Online (Sandbox Code Playgroud) 我有一组假装付款的状态,每个都有付款ID.
我想获取每个付款ID的最新状态.测试我创建了一些虚拟数据然后尝试查询它.我到目前为止:
[Test]
public void GetPaymentLatestStatuses()
{
var client = new TestMongoClient();
var database = client.GetDatabase("payments");
var paymentRequestsCollection = database.GetCollection<BsonDocument>("paymentRequests");
var statusesCollection = database.GetCollection<BsonDocument>("statuses");
var payment = new BsonDocument { { "amount", RANDOM.Next(10) } };
paymentRequestsCollection.InsertOne(payment);
var paymentId = payment["_id"];
var receivedStatus = new BsonDocument
{
{ "payment", paymentId },
{ "code", "received" },
{ "date", DateTime.UtcNow }
};
var acceptedStatus = new BsonDocument
{
{ "payment", paymentId },
{ "code", "accepted" },
{ "date", DateTime.UtcNow.AddSeconds(-1) }
};
var completedStatus …Run Code Online (Sandbox Code Playgroud) c# mongodb mongodb-query aggregation-framework mongodb-.net-driver
我正在使用C#MongoDB驱动程序,并且要保存以下相当复杂的JSON结构:
{
"name" : "value",
"age": 1,
"isFemale": true,
"Hobbies" : {
//All data within the "Hobbies" node is dynamic
//and may change from one item to another.
"stringItem" : "value",
"intItem" : 0.0,
"listOfItems" : [
{ "field" : 1696.0 }
],
"intArray" : [ 566.0, 1200.0 ]
},
"Collection" : [
//All data within the "Collection" node is dynamic
//and may change from one item to another.
{
"field" : "string",
"FieldTypeId" : 2.0,
"array" : [
{ …Run Code Online (Sandbox Code Playgroud) mongodb ×10
c# ×8
.net ×2
async-await ×1
c#-4.0 ×1
geocoding ×1
geospatial ×1
json ×1
linq ×1
mongoid ×1
polymorphism ×1