在C#我怎样才能last N插入从集合文件中mongo?
在Mongodb v2.2中当我尝试将一个简单的json文档文件从我的.json文件导入到空集合中时,我获得了13个对象导入.这就是我正在做的事情.
这是数据(我缩短了字段名称来保护数据):
[
{
"date" : ISODate("2012-08-01T00:00:00Z"),
"start" : ISODate("2012-08-01T00:00:00Z"),
"xxx" : 1,
"yyt" : 5,
"p" : 6,
"aam" : 20,
"dame" : "denon",
"33" : 10,
"xxt" : 8,
"col" : 3,
"rr" : [
{ "name" : "Plugin 1", "count" : 1 },
{ "name" : "Plugin 2", "count" : 1 },
{ "name" : "Plugin 3", "count" : 1 }
],
"xkx" : { "y" : 0, "n" : 1 },
"r" : { "y" …Run Code Online (Sandbox Code Playgroud) 我有这个代码从mongofb整理中删除项目
private MongoCollection<T> GetCollection()
{
connectionString = "mongodb://localhost/?safe=true";
server = MongoServer.Create(connectionString);
database = server.GetDatabase("CSCatalog");
return database.GetCollection<T>("myCollectionName");
}
public bool Delete(T entity)
{
var id = typeof(T).GetProperty("Id").GetValue(entity,null).ToString();
var query = Query.EQ("_id",id);
var finded = GetCollection().Find(query); // return null
var result= GetCollection().Remove(query, MongoDB.Driver.RemoveFlags.Single); // no errors, but don't remove
return esito.Ok; //return true but donn't remove.
}
Run Code Online (Sandbox Code Playgroud)
GetCollection()方法检索正确的集合,我测试了它的宽度调试.在集合中有我想删除的项目,它具有我在第一行中重新获得的相同ID.
该实体有一些字段和一个名为"Id"的Objectid字段
我正在尝试查询mongoDB,我收到以下错误:
无法确定表达式的序列化信息:c.IndexMetadata.Indexed.HasValue.
其中Indexed是可以为空的日期时间
我的查询如下:
Collection.AsQueryable<Candidate>(c => !c.IndexMetadata.Indexed.HasValue || c.IndexMetadata.Updated.Value > c.IndexMetadata.Indexed.Value).ToList();
Run Code Online (Sandbox Code Playgroud)
这两个索引和更新,可为空的日期时间类型
我想这是因为HasValue没有直接转换为mongo Query,任何解决方法?
我在MongoDB中有相当复杂的数据结构.我的自定义数组由项目+长度字段表示(这是另一个故事,为什么JSON数组在我的特定情况下不起作用).保存和更新长度字段的代码是通用的,始终提供整数,而不是浮点数或双精度数.长度字段的值一直是32位整数,直到昨天我未能将其中一个转换为int32实际的double.
所以我有一千个有问题的文件(所有相同的层次结构)和其他一千个.首先,我将其导出为JSON.有问题的字段的值是56,在JSON中它看起来像:
...
"55": {
...
},
"Length": 56
},
...
Run Code Online (Sandbox Code Playgroud)
然后我将JSON导入到本地MongoDB服务器进行调查,当我在C#驱动程序中获取其长度字段时,我的类型是double.同一文档中的其他长度字段是整数.这让我怀疑是这个讨厌行为的C#驱动程序.要分离MongoDB服务器和C#驱动程序,我需要在MongoDB服务器中检查此值类型,但我不知道如何.试
typeof db.collection.findOne({...}, {"path.to.value": 1}).path.to.value
Run Code Online (Sandbox Code Playgroud)
打印number所有长度值.但我不相信MongoDB,因为简单的测试:
db.test.save({fint: NumberInt(0)})
typeof db.test.findOne().fint
Run Code Online (Sandbox Code Playgroud)
打印number也是.
根据MongoDB BSON类型,内部类型是双精度和整数,它只在MongoDB Shell中看起来像numbers.
现在,主要问题是你有没有见过类似的东西或者有什么问题?
更具体的问题:如何检查确切的内部数据类型的值?
MongoDB 2.4.6,C#驱动程序1.8.1,可在Windows和Linux上看到
UPDATE
问题出在完全不同的地方.我很困惑,从JSON导入文档(通过JavaScript)使所有数字都成为doubleMongoDB内部类型.接受的答案帮助我找到了答案.
我有一个Mongo Collection的TaskBase文件。TaskBase有三个子类。我为此集合创建了一个集合管理器(从我已经使用的通用管理器中)。当我创建,更新或检索TaskBase我的子类时,我将获得正确的类型,也不例外。
我创建了以下方法:
public IEnumerable<TaskBase> GetTasksByAppId(string appId)
{
var entityQuery = Query<TaskBase>.EQ(t => t.AppOId, appId);
return this.MongoConnectionHandler.MongoCollection.Find(entityQuery).ToList();
}
Run Code Online (Sandbox Code Playgroud)
当我运行此程序时,我得到一个例外,Element [some element existing only in a subclass] is not a property or member of TaskBase我理解为什么会收到此例外,我只是不知道该怎么办。我想收集可以与某个应用程序关联的所有任务类型的集合。
我在PC-BSD 10.1上使用MonoDevelop并使用MongoDB 3.2.我从Nuget下载了MongoDB.Driver(+ Bson&Core).我可以进行基本的读写操作,并试图通过遵循StackOverflow中最新的示例来使GridFS工作:
使用C#的MongoDB GridFs,如何存储图像等文件?
首先,我的系统无法识别(貌似)静态MongoServer类,因此我切换到MognoClient来获取数据库.然后我得到以下内容:
"类型MongoDB.Driver.IMongoDatabase' does not contain a definition forGridFS的'没有扩展方法GridFS' of typeMongoDB.Driver.IMongoDatabase’可以找到."
using System;
using System.IO;
using MongoDB;
using MongoDB.Driver;
using MongoDB.Driver.Core;
using MongoDB.Bson;
//using MongoDB.Driver.GridFS; -> an attempt to use the legacy driver.
namespace OIS.Objektiv.SocketServer
{
public class Gridfs
{
public Gridfs ()
{
var server = MongoServer.Create("mongodb://localhost:27017");
var database = server.GetDatabase("test");
// var client = new MongoClient("mongodb://localhost:27017");
// var database = client.GetDatabase("test");
var fileName = "D:\\Untitled.png";
var newFileName = "D:\\new_Untitled.png"; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用IRepository模式(C#,MVC5)创建一个MongoDB Web应用程序,以便更容易进行单元测试.只是想知道是否有人可以告诉我为什么这更快.
这是使用最新的MongoDB c#驱动程序.
在我的IRepository类中,我有以下内容
IQueryable<T> SearchFor();
List<T> SearchFor(FilterDefinition<T> filter);
Run Code Online (Sandbox Code Playgroud)
找到一个SO帖子,建议使用IQueryable来提高使用IEnumerable的速度.
这是MongoRepository类的代码.
public IQueryable<T> SearchFor() {
return _collection.AsQueryable<T>();
}
public List<T> SearchFor(FilterDefinition<T> filter) {
return _collection.Find(filter).ToList();
}
Run Code Online (Sandbox Code Playgroud)
据我所知,过滤器定义是您通常如何将查询编码到数据库.
以下是从数据库获取数据的调用
IQueryable<Client> asd4 = collection.SearchFor().Where(x => x.ClientDesc == "<search text>");
FilterDefinition<Client> filter1 = Builders<Client>.Filter.Eq("ClientDesc", "<search text>");
List<Client> asd10 = collection.SearchFor(filter1).ToList<Client>();
Run Code Online (Sandbox Code Playgroud)
请注意,我知道我应该使用IQueryable和Linq路由只是因为IRepository不应该包含技术相关的classess(如FilterDefinition).
当针对具有30k简单文档的集合进行测试并测试不同方法的速度时,我得到以下结果.
使用IQueryable在3ms内完成,FilterDefinition在43ms内完成.
我想知道为什么对IQueryable的Linq查询比使用API发送请求只是为了返回特定值更快?
更新:根据@lenkan的建议,我为IQueryable的每个循环添加了一个.
public void PerformanceTest(IRepository<Client> collection) {
Stopwatch sw = new Stopwatch();
// Delete all records
// ******************
System.Diagnostics.Debug.WriteLine("*****************");
sw.Start();
collection.DeleteAll();
sw.Stop();
System.Diagnostics.Debug.WriteLine("Deleting all records: " + sw.Elapsed);
// …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种有条件地结合Set操作的方法。此刻,我无法递增到updatedefinitions上,而又没有一个接一个的点。
例如。代替:
Builders<BsonDocument>.Update.Set("someElement.Length", b.Length)
.Set("someElement.Path", b.Path)
Run Code Online (Sandbox Code Playgroud)
我试图找到一种徒然使用某种东西的方法:
var update = Builders<BsonDocument>.Update;
bool hasChanged = false;
if (a.Length != b.Length)
{
hasChanged = true;
update.Set("someElement.Length", b.Length)
}
if (a.Path != b.Path)
{
hasChanged = true;
update.Set("someElement.Path", b.Path)
}
if (hasChanged)
await someCollection.UpdateOneAsync(Builders<someModel>.Filter.Eq("_id", a.Id), update);
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,还是我在天上追馅饼?我不想替换整个文档,并且只希望更新已更改的字段。
public class BsonObjectAttribute: BsonRepresentationAttribute
{
public BsonObjectAttribute(BsonType representation)
{
base(representation);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试从创建属性BsonRepresentationAttribute。但是我遇到了以下两个编译错误
没有给出与BsonRepresentationAttribute.BsonRepresentationAttribute(BsonType)的所需形式参数“ representation”相对应的参数。
和
在此情况下,关键字“ base”的使用无效
mongodb ×9
c# ×6
.net ×3
attributes ×1
bson ×1
c#-3.0 ×1
collections ×1
datetime ×1
find ×1
freebsd ×1
linq ×1
mongoid ×1
monodevelop ×1
polymorphism ×1