1

i have ravendb document called Orders like this

{
"MyOrders": [
    "S1",
    "S2"
],

"Id": "6666"
}

I will query this document and pass order string for example S1 Then it should return me that document as S1 matches it. I am pretty new to this ravendb. I am unable to find way. I have written only this so far

   public class MyOrderIndexes: AbstractIndexCreationTask<Order>
{

    public MyOrderIndexes()
    {
        Map = Orders => from Order in Orders
                                 select new
                                             {
                                                 ID= Order.Id
                                             };
        Index(x => x.Id, FieldIndexing.NotAnalyzed);
    }
}

can someone help me

2 Answers 2

2

To query that, you don't need to create an index. Just do a query, Raven will create it for you.

This query should work just fine:

var hasS2 = session.Query<Orders>()
   .Where(o => o.MyOrders.Contains("S2"))
   .ToList();
Sign up to request clarification or add additional context in comments.

8 Comments

So when we need to write index?
it complains that it cannot understand expression .Where(o => o.MyOrders.Any(myO => myO == "S2"))
Try use .Contains instead. I've updated the post with the code sample.
>> "When do we need to write an index?" If you need special handling for fields at index time. For example, maybe you want to add spatial search data for a particular field, so that you can later search by geographic coordinates. Indexes are useful for such scenarios. But I recommend you let Raven handle indexes; my personal approach is, "Unless there's a special reason to write an index, don't. Let Raven handle it."
can you help me with index as i have pressure to write index and i am still struggling
|
0

One problem is the FieldIndexing on Id, and also that you need to map MyOrders to perform query on it.

Index:

public class MyOrderIndexes: AbstractIndexCreationTask<Order>
{    
    public MyOrderIndexes()
    {
        Map = Orders => from Order in Orders
                                 select new
                                 {
                                       Id = Order.Id,
                                       MyOrders = Order.MyOrders
                                 };
    }
}

Query:

var results = session
    .Query<Order, MyOrderIndexes>()
    .Where(x => x.MyOrders.Any(l => l == "S1"))
    .ToList();  

Model:

public class Order
{
    public string Id { get; set; }
    public ICollection<string> MyOrders { get; set; }
}

4 Comments

i have like this public List<string> MyOrders { get; private set; }
remove the private
what we acheive doing so and what is impact?
i'm not sure that RavenDB can set MyOrders with private set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.