public static BsonDocument ToMongoCommit(this Commit commit, IDocumentSerializer serializer) { int streamRevision = commit.StreamRevision - (commit.Events.Count - 1); IEnumerable <BsonDocument> events = commit .Events .Select(e => new BsonDocument { { "StreamRevision", streamRevision++ }, { "Payload", new BsonDocumentWrapper(typeof(EventMessage), serializer.Serialize(e)) } }); return(new BsonDocument { { MongoFields.Id, new BsonDocument { { MongoFields.BucketId, commit.BucketId }, { MongoFields.StreamId, commit.StreamId }, { MongoFields.CommitSequence, commit.CommitSequence } } }, { MongoFields.CommitId, commit.CommitId }, { MongoFields.CommitStamp, commit.CommitStamp }, { MongoFields.Headers, BsonDocumentWrapper.Create(commit.Headers) }, { MongoFields.Events, new BsonArray(events) }, { MongoFields.Dispatched, false } }); }
/// <summary> /// 更新 /// </summary> /// <param name="id"></param> /// <param name="vehicleItem"></param> public void Put(int id, [FromBody] VehicleItem vehicleItem) { var readPreference = new ReadPreference(ReadPreferenceMode.SecondaryPreferred); MongoDatabaseSettings mdSetting = new MongoDatabaseSettings(); mdSetting.ReadPreference = readPreference; var connectionString = ConfigurationManager.AppSettings["dbCon"]; var database = ConfigurationManager.AppSettings["dbName"]; MongoClient client = new MongoClient(connectionString); //连接mogodb数据库 IMongoDatabase db = client.GetDatabase(database, mdSetting); //数据库 List <WriteModel <BsonDocument> > requests = new List <WriteModel <BsonDocument> >(); BsonDocument query = new BsonDocument("citycode", id); //var update = new BsonDocument() { { "$set", BsonDocumentWrapper.Create(new { citycode = vehicleItem.citycode , speed = vehicleItem.speed}) } }; var update = new BsonDocument() { { "$set", BsonDocumentWrapper.Create(vehicleItem) } }; //requests.Add(new UpdateOneModel<BsonDocument>(query, update) { IsUpsert = true });//存在则更新,不存在则新增,更新单个符合查询条件的数据 requests.Add(new UpdateManyModel <BsonDocument>(query, update) { IsUpsert = true }); //存在则更新,不存在则新增,更新多个符合查询条件的数据 if (requests.Count > 0) { db.GetCollection <BsonDocument>("tblCrud").BulkWrite(requests); } }
public void TestCreateGenericWithValue() { var wrapper = BsonDocumentWrapper.Create <C>(_c); var expected = "{ \"X\" : 1 }"; Assert.AreEqual(expected, wrapper.ToJson()); }
public void TestCreateWithNominalTypeAndValue() { var wrapper = BsonDocumentWrapper.Create(typeof(C), _c); var expected = "{ \"X\" : 1 }"; Assert.AreEqual(expected, wrapper.ToJson()); }
/// <summary> /// Finds the and modify. /// </summary> /// <param name="id"> </param> /// <param name="event">The @event.</param> /// <returns></returns> public EntityEventSource <TEntity, TId> AddEvent(TId id, IDomainEvent <TEntity> @event) { _repository.Create(new EntityEventSource <TEntity, TId> { Id = id, Status = "Ready" }); EntityEventSource <TEntity, TId> entity = null; SpinWait.SpinUntil(() => { var query = Query.And(Query.EQ("_id", BsonDocumentWrapper.Create(id)), Query.Or(Query.EQ("Status", "Ready"), Query.LTE("ProcessingAt", new BsonDateTime( DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(_timeOut)))), Query.EQ("ProcessingBy", Thread.CurrentThread.ManagedThreadId.ToString( CultureInfo.InvariantCulture)))); entity = _repository.FindAndModify ( query, x => ((UpdateBuilder)x.GetUpdateBuilder()) .Push("Events", BsonDocumentWrapper.Create(@event)) .Set("Status", new BsonString("Processing")) .Set("ProcessingAt", new BsonDateTime(DateTime.UtcNow)) .Set("ProcessingBy", new BsonString(Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture))), false, true ); return(entity != null); }); return(entity); }
/// <summary> /// Sets a scope that contains variables that can be accessed by the map, reduce and finalize functions. /// </summary> /// <param name="scope">The scope.</param> /// <returns>The builder (so method calls can be chained).</returns> public MapReduceOptionsBuilder SetScope( IMongoScope scope ) { document["scope"] = BsonDocumentWrapper.Create(scope); return(this); }
public void TestCreateWithNominalTypeAndNullValueAndIsUpdateDocument() { var wrapper = BsonDocumentWrapper.Create(typeof(C), null, false); var expected = "null"; Assert.AreEqual(expected, wrapper.ToJson()); }
public static BsonDocument ToMongoCommit(this CommitAttempt commit, LongCheckpoint checkpoint, IDocumentSerializer serializer) { int streamRevision = commit.StreamRevision - (commit.Events.Count - 1); int streamRevisionStart = streamRevision; IEnumerable <BsonDocument> events = commit .Events .Select(e => new BsonDocument { { MongoCommitFields.StreamRevision, streamRevision++ }, { MongoCommitFields.Payload, new BsonDocumentWrapper(typeof(EventMessage), serializer.Serialize(e)) } }); return(new BsonDocument { { MongoCommitFields.CheckpointNumber, checkpoint.LongValue }, { MongoCommitFields.CommitId, commit.CommitId }, { MongoCommitFields.CommitStamp, commit.CommitStamp }, { MongoCommitFields.Headers, BsonDocumentWrapper.Create(commit.Headers) }, { MongoCommitFields.Events, new BsonArray(events) }, { MongoCommitFields.Dispatched, false }, { MongoCommitFields.StreamRevisionFrom, streamRevisionStart }, { MongoCommitFields.StreamRevisionTo, streamRevision - 1 }, { MongoCommitFields.BucketId, commit.BucketId }, { MongoCommitFields.StreamId, commit.StreamId }, { MongoCommitFields.CommitSequence, commit.CommitSequence } }); }
public void TestCreateGenericWithNullValueAndIsUpdateDocument() { var wrapper = BsonDocumentWrapper.Create <C>(null, false); var expected = "null"; Assert.AreEqual(expected, wrapper.ToJson()); }
public static BsonDocument ToMongoCommit(this CommitAttempt commit, Int64 checkpoint, IDocumentSerializer serializer) { int streamRevision = commit.StreamRevision - (commit.Events.Count - 1); int streamRevisionStart = streamRevision; IEnumerable <BsonDocument> events = commit .Events .Select(e => new BsonDocument { { MongoCommitFields.StreamRevision, streamRevision++ }, { MongoCommitFields.Payload, BsonDocumentWrapper.Create(typeof(EventMessage), serializer.Serialize(e)) } }); var mc = new MongoCommit { CheckpointNumber = checkpoint, CommitId = commit.CommitId, CommitStamp = commit.CommitStamp, Headers = commit.Headers, Events = new BsonArray(events), StreamRevisionFrom = streamRevisionStart, StreamRevisionTo = streamRevision - 1, BucketId = commit.BucketId, StreamId = commit.StreamId, CommitSequence = commit.CommitSequence }; return(mc.ToBsonDocument()); }
public static BsonDocument ToMongoCommit_original(this CommitAttempt commit, Int64 checkpoint, IDocumentSerializer serializer) { int streamRevision = commit.StreamRevision - (commit.Events.Count - 1); int streamRevisionStart = streamRevision; IEnumerable <BsonDocument> events = commit .Events .Select(e => new BsonDocument { { MongoCommitFields.StreamRevision, streamRevision++ }, { MongoCommitFields.Payload, BsonDocumentWrapper.Create(typeof(EventMessage), serializer.Serialize(e)) } }); //var dictionarySerialize = new DictionaryInterfaceImplementerSerializer<Dictionary<string, object>>(DictionaryRepresentation.ArrayOfArrays); //var dicSer = BsonSerializer.LookupSerializer<Dictionary<string, object>>(); return(new BsonDocument { { MongoCommitFields.CheckpointNumber, checkpoint }, { MongoCommitFields.CommitId, commit.CommitId }, { MongoCommitFields.CommitStamp, commit.CommitStamp }, { MongoCommitFields.Headers, BsonDocumentWrapper.Create(commit.Headers) }, { MongoCommitFields.Events, new BsonArray(events) }, { MongoCommitFields.StreamRevisionFrom, streamRevisionStart }, { MongoCommitFields.StreamRevisionTo, streamRevision - 1 }, { MongoCommitFields.BucketId, commit.BucketId }, { MongoCommitFields.StreamId, commit.StreamId }, { MongoCommitFields.CommitSequence, commit.CommitSequence } }); }
/// <summary> /// Sets the sort order (useful in combination with SetLimit, your map function should not depend on the order the documents are sent to it). /// </summary> /// <param name="sortBy">The sort order.</param> /// <returns>The builder (so method calls can be chained).</returns> public MapReduceOptionsBuilder SetSortOrder( IMongoSortBy sortBy ) { document["sort"] = BsonDocumentWrapper.Create(sortBy); return(this); }
/// <summary> /// Sets the optional query that filters which documents are sent to the map function (also useful in combination with SetSortOrder and SetLimit). /// </summary> /// <param name="query">The query.</param> /// <returns>The builder (so method calls can be chained).</returns> public MapReduceOptionsBuilder SetQuery( IMongoQuery query ) { document["query"] = BsonDocumentWrapper.Create(query); return(this); }
private static BsonValue Mangle(this IDictionary <string, object> headers) { if (headers.Any(h => h.Key.Contains("."))) { return(new BsonArray(headers.Select(h => new BsonArray(new [] { h.Key, h.Value })))); } return(BsonDocumentWrapper.Create(headers)); }
/// <summary> /// Adds an $addToSet update modifier. /// </summary> /// <param name="name">The name of the array element.</param> /// <param name="value">The wrapped value to add to the set.</param> /// <returns>The builder (so method calls can be chained).</returns> public UpdateBuilder AddToSetWrapped <T>( string name, T value ) { var wrappedValue = (BsonValue)BsonDocumentWrapper.Create(value); // the cast to BsonValue is required return(AddToSet(name, wrappedValue)); }
public string Serialize(object @object) { var ser = BsonDocumentWrapper.Create(@object).ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }); return(ser); }
/// <summary> /// Sets the sort order for the server to sort the documents by before returning them. /// </summary> /// <param name="sortBy">The sort order.</param> /// <returns>The cursor (so you can chain method calls to it).</returns> public virtual MongoCursor SetSortOrder(IMongoSortBy sortBy) { if (_isFrozen) { ThrowFrozen(); } SetOption("$orderby", BsonDocumentWrapper.Create(sortBy)); return(this); }
/// <summary> /// Gets the query. /// </summary> /// <param name="snapShotType">Type of the snap shot.</param> /// <param name="entity">The entity.</param> /// <returns></returns> public IMongoQuery GetQuery(SnapShotType snapShotType, TEntity entity) { return(snapShotType == SnapShotType.ById ? Query.EQ("_id", BsonDocumentWrapper.Create(entity.Id)) : Query.And(Query.EQ("_id", BsonDocumentWrapper.Create(entity.Id)), Query.Or(Query.NotExists("Version"), Query.LTE("Version", BsonDocumentWrapper.Create(entity.Version))) )); }
/// <summary> /// Sets the sort order for the server to sort the documents by before returning them. /// </summary> /// <param name="sortBy">The sort order.</param> /// <returns>The cursor (so you can chain method calls to it).</returns> public virtual MongoCursor SetSortOrder(IMongoSortBy sortBy) { if (sortBy == null) { throw new ArgumentNullException("sortBy"); } if (_isFrozen) { ThrowFrozen(); } SetOption("$orderby", BsonDocumentWrapper.Create(sortBy)); return this; }
public BsonArray ConvertToBsonArray(ICollection <T> set) { var bson = new BsonArray(); foreach (var item in set) { bson.Add(BsonDocumentWrapper.Create(typeof(T), item)); } return(bson); }
public BsonArray ConvertToBsonArray(IList <T> list) { var bson = new BsonArray(); foreach (var item in list) { bson.Add(BsonDocumentWrapper.Create(typeof(T), item)); } return(bson); }
public static BsonDocument ToMongoSnapshot(this Snapshot snapshot, IDocumentSerializer serializer) { return(new BsonDocument { { "_id", new BsonDocument { { "StreamId", snapshot.StreamId }, { "StreamRevision", snapshot.StreamRevision } } }, { "Payload", BsonDocumentWrapper.Create(serializer.Serialize(snapshot.Payload)) } }); }
public BsonDocument ConvertToBsonDocument(IDictionary <TKey, TValue> dictionary) { var doc = new BsonDocument(); foreach (var item in dictionary) { doc.Add(item.Key.ToString(), BsonDocumentWrapper.Create(item.Value)); } return(doc); }
/// <summary> /// Returns the number of documents that match the query (ignores Skip and Limit, unlike Size which honors them). /// </summary> /// <returns>The number of documents that match the query.</returns> public virtual int Count() { isFrozen = true; var command = new CommandDocument { { "count", collection.Name }, { "query", BsonDocumentWrapper.Create(query) } // query is optional }; var result = database.RunCommand(command); return(result.Response["n"].ToInt32()); }
/// <summary> /// Returns the number of documents that match the query (ignores Skip and Limit, unlike Size which honors them). /// </summary> /// <returns>The number of documents that match the query.</returns> public virtual long Count() { _isFrozen = true; var command = new CommandDocument { { "count", _collection.Name }, { "query", BsonDocumentWrapper.Create(_query), _query != null } // query is optional }; var result = _database.RunCommand(command); return(result.Response["n"].ToInt64()); }
public void Serialize <TView>(TView entity, Stream stream) { var json = BsonDocumentWrapper.Create((object)entity).ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }); using (var sw = new StreamWriter(stream)) { sw.Write(json); } }
public static BsonDocument ToMongoSnapshot(this Snapshot snapshot, IDocumentSerializer serializer) { return(new BsonDocument { { MongoFields.Id, new BsonDocument { { MongoFields.BucketId, snapshot.BucketId }, { MongoFields.StreamId, snapshot.StreamId }, { MongoFields.StreamRevision, snapshot.StreamRevision } } }, { MongoFields.Payload, BsonDocumentWrapper.Create(serializer.Serialize(snapshot.Payload)) } }); }
/// <summary> /// Saves the snap shot. /// </summary> /// <param name="entity">The entity.</param> /// <param name="snapShotType">Type of the snap shot.</param> /// <param name="events">The events.</param> /// <param name="state">The state.</param> public void SaveSnapShot(TEntity entity, SnapShotType snapShotType, IEnumerable <IDomainEvent <TEntity> > events, string state) { _repository.Update ( _mongoBuilder.GetQuery(snapShotType, entity), x => ((UpdateBuilder)x.GetUpdateBuilder()) .Set("SnapShot", BsonDocumentWrapper.Create(entity)) .Set("Version", BsonDocumentWrapper.Create(entity.Version)) .Set("State", BsonDocumentWrapper.Create(state)) .PullAll("Events", events.Select(BsonDocumentWrapper.Create)) .Set("AppliedEvents", BsonDocumentWrapper.Create(events)) ); }
/// <summary> /// Returns the size of the result set (honors Skip and Limit, unlike Count which does not). /// </summary> /// <returns>The size of the result set.</returns> public virtual int Size() { isFrozen = true; var command = new CommandDocument { { "count", collection.Name }, { "query", BsonDocumentWrapper.Create(query) }, // query is optional { "limit", limit, limit != 0 }, { "skip", skip, skip != 0 } }; var result = database.RunCommand(command); return(result.Response["n"].ToInt32()); }
public void TestCreateGenericWithObject() { var c = CreateC(); var wrapper = BsonDocumentWrapper.Create <C>(c); Assert.Same(BsonSerializer.LookupSerializer(typeof(C)), wrapper.Serializer); Assert.Same(c, wrapper.Wrapped); Assert.Equal(false, wrapper.IsMaterialized); wrapper = BsonDocumentWrapper.Create <C>(null); Assert.Same(BsonSerializer.LookupSerializer(typeof(C)), wrapper.Serializer); Assert.Same(null, wrapper.Wrapped); Assert.Equal(false, wrapper.IsMaterialized); }