Getting metadata info from a document

77 views
Skip to first unread message

Jeremy Holt

unread,
Jul 20, 2021, 12:40:14 PM7/20/21
to RavenDB - an awesome database
I am trying to get the count of the attachments for a document where I apply a criteria to the count.
In pseudo-code
var counts = document.getMetaData("@attachments").where(c=>c.Name.Contains(filter))

I can't use session.Advanced.GetMetaDataFor(document.Id)["@attachments"].

I have tried this code (straight out of the documentation), but cannot work out how to get and filter on the 'Name' property of the 'attachment' 

Basically I want to cast 'attachment' to something I can manipulate

var command = new GetDocumentsCommand(contract.Id, null, metadataOnly: true);
_session.Advanced.RequestExecutor.Execute(command,_session.Advanced.Context);
var result = (BlittableJsonReaderObject)command.Result.Results[0];
var documentMetaData = (BlittableJsonReaderObject)result["@metadata"];
if (documentMetaData["@attachments"] is BlittableJsonReaderArray attachments)
{
foreach(var attachment in attachments)
{
var propertyNames= attachment.GetType().GetProperties().Select(x => x.Name).ToList();
Console.WriteLine( propertyNames  );
}
}

Igal Merhavia

unread,
Jul 21, 2021, 2:47:31 AM7/21/21
to rav...@googlegroups.com
Hi,

If all you need is the attachment names, you can use `session.Advanced.Attachments.GetNames(entity)` - https://ravendb.net/docs/article-page/4.2/csharp/client-api/session/attachments/loading.
That will read the names from the metadata.
If you want to get the metadata you can use `session.Advanced.GetMetadataFor(entity)` - https://ravendb.net/docs/article-page/5.0/csharp/client-api/session/how-to/get-and-modify-entity-metadata.

Best regards,
Igal

--
You received this message because you are subscribed to the Google Groups "RavenDB - an awesome database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/77853bb3-7eac-463a-b07d-1d92d1922665n%40googlegroups.com.

Jeremy Holt

unread,
Jul 21, 2021, 10:51:31 AM7/21/21
to RavenDB - an awesome database
Hi, 
I'm aware of getMetadataFor and GetNames(), but the problem is that I have to access the number of attachments before I have access to the entity.

The controller/user is sending me back the entity with only the Clr-Raven and Collections metadata, and I want to avoid reloading the original entity.

That is why I was going after getting the metadataOnly using the GetDocumentsCommand, but fell flat when I couldn't work out how to manipulate the object it sends back.

In a query of the same data I cast to a JArray which works fine. However I can't work out what to do with the BlittableJsonReaderArry.

```
var list = await query.Select(c => new SignedContractListItem
{
Id = c.Id,
AttachmentCount = (RavenQuery.Metadata(c)["@attachments"] as JArray).Count(x => x["Name"].Contains("root!")),
Complete = c.Containers.All(x => x.Status == ContainerStatus.Shipped)
}).OrderBy(c => c.Id)
.ToListAsync();
```

Oren Eini (Ayende Rahien)

unread,
Jul 21, 2021, 11:02:44 AM7/21/21
to ravendb
I would strongly recommend just using:

s.Advanced.Attachments.GetNames(doc)

That is much simpler.

You can use:

var result = (BlittableJsonReaderObject)command.Result.Results[0];
var documentMetaData = (BlittableJsonReaderObject)result["@metadata"];
if (documentMetaData["@attachments"] is BlittableJsonReaderArray attachments)
{
    foreach(BlittableJsonReaderObject attachment in attachments)
    {
        attachment.TryGet("Name", out string name);
    }
}

Here is the relevant code from GetNames(), which only deal with the metadata:



--
Oren Eini
CEO   /   Hibernating Rhinos LTD
Skype:  ayenderahien
Support:  sup...@ravendb.net
  
Reply all
Reply to author
Forward
0 new messages