简体   繁体   中英

C# MongoDriver Lookup

Im trying to do lookup on my mongo collections.

I have a collection called wishlists and collection named wishes.

Each wish has reference to wishlist called wishlistId.

Im trying to get all wishlists with its wishes for the user with id = userId.

Here is my query (wishlistService.GetAll()):

return this.collection.Aggregate().Match(Builders<WishlistModel>.Filter.Where(o => o.userId == userId)).Lookup("wishes", "_id", "wishlistId", "result").ToListAsync();

When I look at the result using debugger it has the correct data somewhere inside it, but when im trying to return this as Ok result, it will fail.

var userId = User.Identity.Name;
List<BsonDocument> wishlists = await this.wishlistsService.GetAll(userId);
return Ok(wishlists);

The error in Angular http request is:

GET http://127.0.0.1:5000/api/wishlists net::ERR_INCOMPLETE_CHUNKED_ENCODING

This is what I see in debugger when viewing wishlists variable. 在此处输入图片说明

Im sure Im doing something wrong when querying the data, but im not sure what as im new to .NET

After a day of fiddling Ive found the correct way to do this.

You have to set up the strict mode for JsonSerializer.

var jsonWriterSettings = new MongoDB.Bson.IO.JsonWriterSettings { OutputMode = MongoDB.Bson.IO.JsonOutputMode.Strict };

and then serialize the results from the service in controller.

return Ok(wishlists.ToJson(jsonWriterSettings));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM