mongodb-.net-driver

How to decorate a class item to be an index and get the same as using ensureIndex?

折月煮酒 提交于 2019-12-21 20:35:38
问题 I'd like to define in class declaration which items are index, something like: public class MyClass { public int SomeNum { get; set; } [THISISANINDEX] public string SomeProperty { get; set; } } so to have the same effect as ensureIndex("SomeProperty") Is this possible? 回答1: I think this is a nice idea, but you have to do this yourself, there's no built-in support for it. If you have an access layer you can do it in there. You'd need an attribute class, something like this; public enum

What are drawbacks of storing Guid as String in MongoDB?

核能气质少年 提交于 2019-12-21 07:55:41
问题 An application persists Guid field in Mongo and it ends up being stored as BinData: "_id" : new BinData(3, "WBAc3FDBDU+Zh/cBQFPc3Q==") The advantage in this case is compactness, the disadvantage shows up when one needs to troubleshoot the application. Guids are passed via URLs, and constantly transforming them to BinData when going to Mongo console is a bit painful. What are drawbacks of storing Guid as string in addition to increase in size? One advantage is ease of troubleshooting: "_id" :

Update nested array document

北慕城南 提交于 2019-12-21 05:38:16
问题 say i have this model { _id : 1, ref: '1', children: [ { ref:'1.1', grandchildren: [ { ref:'1.1.1', visible: true; } ] } ] } I'm aware that positional operator for nested arrays isn't available yet. https://jira.mongodb.org/browse/SERVER-831 but wondered whether its possible to atomically update the document in the nested array? In my example, i'd like to update the visible flag to false for the document for ref 1.1.1. I have the children record ref == '1.1' and the grandchildrenref == '1.1.1

How to RegisterClassMap for all classes in a namespace for MongoDb?

萝らか妹 提交于 2019-12-21 04:45:07
问题 The MongoDB driver tutorial suggests to register class maps to automap via BsonClassMap.RegisterClassMap<MyClass>(); I would like to automap all classes of a given namespace without explicitly writing down RegisterClassMap for each class. Is this currently possible? 回答1: You don't need write BsonClassMap.RegisterClassMap<MyClass>(); , because all classes will be automapped by default. You should use RegisterClassMap when you need custom serialization: BsonClassMap.RegisterClassMap<MyClass>(cm

MongoDB remove mapreduce collection

老子叫甜甜 提交于 2019-12-21 04:04:39
问题 Due to error in client code, mongodb have created many "mr.mapreduce...." collections, how to remove them all (by mask maybe). 回答1: I run script in interactive shell: function f() { var names = db.getCollectionNames(); for(var i = 0; i < names.length; i++){ if(names[i].indexOf("mr.") == 0){ db[names[i]].drop();}}}; f(); It resolved my problem. 回答2: Temporary map-reduce table should be cleaned up when the connection which created them is closed: map/reduce is invoked via a database command.

Getting an item count with MongoDB C# driver query builder

强颜欢笑 提交于 2019-12-21 04:04:09
问题 Using the c# driver for MongoDB I can easily construct a query against which I can then add SetSkip() and SetLimit() parameters to constrict the result set to a certain size. However I'd like to be able to know what item count of the query would be before applying Skip and Take without executing the query and loading the entire result set (which could be huge) into memory. It looks like I can do this with MongoDB directly through the shell by using the count() command. e.g.: db.item.find( {

Building indexes in MongoDB with .NET driver 2.0

蓝咒 提交于 2019-12-21 03:59:34
问题 What's the new way to build indexes with the new driver 2.0? There's no documentation whatsoever about this. Apparently this now works with the new IndexKeysDefinitionBuilder<> interface but that's all I got so far. 回答1: You need to call and await CreateOneAsync with an IndexKeysDefinition you get by using Builders.IndexKeys : static async Task CreateIndex() { var client = new MongoClient(); var database = client.GetDatabase("db"); var collection = database.GetCollection<Hamster>("collection"

Upserting in Mongo DB and the Id problem

℡╲_俬逩灬. 提交于 2019-12-20 17:39:22
问题 I have a problem while upserting to mongo db using the official C# driver. public abstract class AggregateRoot { /// <summary> /// All mongoDb documents must have an id, we specify it here /// </summary> protected AggregateRoot() { Id = ObjectId.GenerateNewId(); } [BsonId] public ObjectId Id { get; set; } } My entities already have the id-s but I had to create the mongo specific Id for it to work, as all the documents in a collection should have one. Now then I receive a new entity in my

How to make nested queries in MongoDb that works like nested Sql select queries

*爱你&永不变心* 提交于 2019-12-20 12:10:08
问题 I want to make an efficient query in MongoDb to find all users who have their userids listed in a usergroup. Ideally I want to make this as a single request to Mongodb. What I want corresponds to nested selects in SQL. I have tried this in the mongo shell: db.user.save({_id:"u1", Name:"u1 name"}); db.user.save({_id:"u2", Name:"u1 name"}); db.user.save({_id:"u3", Name:"u3 name"}); db.usergroup.save({_id:"g1", Users: ["u2","u3"]}); Now here is the select I want to do, but without hardcoding the

MongoDB C# Driver: Ignore Property on Insert

╄→尐↘猪︶ㄣ 提交于 2019-12-20 10:26:32
问题 I am using the Official MongoDB C# Drive v0.9.1.26831, but I was wondering given a POCO class, is there anyway to ignore certain properties from getting inserted. For example, I have the following class: public class GroceryList { public string Name { get; set; } public FacebookList Owner { get; set; } public bool IsOwner { get; set; } } Is there a way, for the IsOwner to not get inserted when I insert a GroceryList object? Basically, I fetch the object from the database then set the IsOwner