我已经尝试了每种组合来为 Mongo 导入二进制数据,但我无法让它工作。我试过使用new BinData(0, <bindata>),我试过使用
{
"$binary" : "<bindata>",
"$type" : "0"
}
第一个给我一个解析错误。第二个给我一个错误阅读“保留字段名称的无效使用”。
我可以很好地导入其他对象。作为参考,我正在尝试导入 BASE64 编码的图像字符串。这是我正在使用的 JSON 的当前版本:
{"_id" : "72984ce4-de03-407f-8911-e7b03f0fec26","OriginalWidth" : 73, "OriginalHeight" : 150, { "$binary" : "", "$type" : "0" }, "ContentType" : "image/jpeg", "Name" : "test.jpg", "Type" : "5ade8812-e64a-4c64-9e23-b3aa7722cfaa"}
Run Code Online (Sandbox Code Playgroud) 这方面似乎缺乏 mongoDB API 文档。我正在尝试使用聚合函数来获取某个集合中流行标签的数量。这是我希望执行的命令:
db.runCommand(
{ aggregate : "articles",
pipeline : [ { $unwind : "$Tags" },
{ $group : { _id : "$Tags", count : { $sum : 1 } }
} ]});
Run Code Online (Sandbox Code Playgroud)
当我使用 shell 执行此操作时,我得到以下信息:
{
"result": [{
"_id": "2012",
"count": 3
}, {
"_id": "seattle",
"count": 5
}],
"ok": 1
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 c# 4.0,所以我想我更愿意将它作为动态对象取回来,但我会尽我所能......
FWIW,我使用 mongoDB for Windows,32 位,v2.1.1(每晚)
我正在尝试getNextSequence为此链接上的 mongoDB 解释实现一个函数 我正在使用拿铁 C# 驱动程序,但我不确定如何new : true在FindOneAndUpdateOptions
MongoDB 代码
function getNextSequence(name) {
var ret = db.counters.findAndModify(
{
query: { _id: name },
update: { $inc: { seq: 1 } },
new: true,
upsert: true
}
);
return ret.seq;
}
Run Code Online (Sandbox Code Playgroud)
C# 代码
public async Task<long> GetNextObjectSequenceAsync(string objectName)
{
var collection = this.Context.GetCollection<ObjectSequence>("Counters");
var filter = new FilterDefinitionBuilder<ObjectSequence>().Where(x => x.Name == objectName);
var options = new FindOneAndUpdateOptions<ObjectSequence, ObjectSequence>() { IsUpsert = true };
var …Run Code Online (Sandbox Code Playgroud) 我正在更新我的代码以使用 MongoDB 新的异步 API。
我的用法之一是使用以下方法获取集合的数据大小:
return Database.GetCollection("collectionName").GetStats().DataSize
Run Code Online (Sandbox Code Playgroud)
有没有办法像在遗留 API 中那样CollectionStatsResult从对象中获取对象?我现在看到的唯一选择是获取 Json 文档并解析它:IMongoCollectionMongoCollection.GetStats()
var jsonCommand = new JsonCommand<BsonDocument>("{collstats : \"collectionName\"}");
var jsonDocument = await Database.RunCommandAsync(jsonCommand);
return Convert.ToInt64(jsonDocument["size"]);
Run Code Online (Sandbox Code Playgroud) public abstract class GenericRepository<T> : IDisposable, IGenericRepository<T> where T : class
{
protected SphereTripMongoDbContext SphereTripMongoDbContext;
public IMongoCollection<T> MongoCollection { get; set; }
protected GenericRepository(SphereTripMongoDbContext sphereTripMongoDbContext)
{
SphereTripMongoDbContext = sphereTripMongoDbContext;
MongoCollection =
SphereTripMongoDbContext.MongoDatabase.GetCollection<T>(typeof(T).Name);
}
public void Dispose()
{
throw new NotImplementedException();
}
public T GetById(string id)
{
var entity = MongoCollection**.Find(t => t.Id == id)**;
return entity;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试为 MongoDb 编写一个通用的抽象存储库类。由于我在基类中使用通用类型,因此在使用Find方法查找文档时“Id”不可见。不知道如何解决这个问题。
任何帮助,将不胜感激。
IMongoDatabase does not support db.GetStats(); which is deprecated in new version.
I want to try alternate approach to get database stats. I use the following code to run command as we can get the stats from shell:
var client = new MongoClient("mongodb://localhost:27017/analytics");
var db = client.GetDatabase("analytics");
var stats = db.RunCommand<BsonDocument>("db.stats()");
var collectionNames = db.RunCommand<BsonDocument>
("db.getCollectionNames()");
Run Code Online (Sandbox Code Playgroud)
I am getting following error here:
JSON reader was expecting a value but found 'db'.
Need help to execute the command on Mongo database using …
我必须在 mongodb 中存储一个 tiff(标签图像文件格式)或 pdf 扫描文件,这些文件应该可以进行文本搜索。就像如果我们想“基于文本”搜索它应该能够搜索 .
我将使用 .net mvc 或 java 和 mongodb 。
那么我如何存储这个 pdf 文件,然后可以从数据库中检索。
任何建议将不胜感激。
谢谢
mongodb mongodb-query spring-data-mongodb mongodb-.net-driver
我有数千个文档的集合,在文档中有一个名为Rate的字段,问题是当前它的类型是字符串,所以当它不可用时,老开发人员将其设置为“N/A”。现在我想在 C# 中将此字段的类型更改为数字(当 n/a 时将其设置为 0),但如果这样做,我将无法加载过去的数据。我们可以自定义反序列化以便将 N/A 转换为 0 吗?
我有以下文档架构。
{
"name":"Name",
"region":"New Jersey",
"address":"92 Something Rd",
"city":"Jersey City",
"state":"NJ",
"zipCode":"07302",
"country":"USA",
amenities":[
"Sauna",
"Locker",
"Shop"
],
"services":[
"Car Rental",
"Transportation"
]
}
Run Code Online (Sandbox Code Playgroud)
我想通过对服务器的一次调用来获取与任何过滤器参数匹配的所有文档,其中映射 1-1 的含义,"state" = "NJ" OR "city" = "Jersey City"而且当列表的任何值包含在任何文档数组子级 example 中时[ "Sauna", "Locker" ] ANY IN "amenities"。它应该是所有可能过滤器的 OR 串联。
使用 C# MongoDB 驱动程序,到目前为止,我在一个MongoRepository类中提出了以下方法,但没有返回所需的结果。
public async Task<IEnumerable<T>> DocumentsMatchEqFieldValueAsync<T>(string collectionName,
IDictionary<string, string> fieldsValues = null,
IDictionary<string, IEnumerable<string>> fieldsWithEnumerableValues = null,
IEnumerable<ObjectId> ids = null)
{
var cursor = await GetEqAsyncCursor<T>(collectionName, fieldsValues, …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据 ID 位于特定 ID 集合中的项目从集合中获取值。
我当前构建过滤器的代码是:
IEnumerable<string> IDList;
using (var enumerator = IDList.GetEnumerator())
{
if (enumerator.MoveNext() == false) return null; // empty collection
// take the first key
var key = enumerator.Current;
filter = Builders<MyClass>.Filter.Eq(p => p.Key, key);
// take all the other keys
while (enumerator.MoveNext())
{
var innerKey = enumerator.Current;
filter = filter | Builders<MyClass>.Filter.Eq(p => p.Key, innerKey);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我获取物品的代码是:
List<MyClass> values = new List<MyClass>();
using (var cursor = await MyCollection.FindAsync(filter))
{
while (await cursor.MoveNextAsync())
{
values.AddRange(cursor.Current);
} …Run Code Online (Sandbox Code Playgroud)