jojacafe
6/22/2016 - 8:15 AM

Query where contains or start with

Query where contains or start with

var query = Query.And(
		Query.Matches("Code", new BsonRegularExpression("/^" + request.Hotel.Code + ".*/i")),
		Query.Matches("Name", new BsonRegularExpression("/.*" + request.Hotel.Name + ".*/ i"))
	);

if (request.Hotel.Code == 0)
{
	query = Query.And(
		Query.Matches("Name", new BsonRegularExpression("/.*" + request.Hotel.Name + ".*/ i"))
	);
}

 MongoCursor<BsonDocument> cursor = mongoCollection.Find(query);

var hotelRet = cursor.OfType<BsonDocument>().Select(m =>
{
	return new Hotel()
	{
		Code = int.Parse(m["Code"].AsString),
		Name = m["Name"].AsString
	};
}).ToList();