MongoDB.Bson.Path 1.0.0

Extension methods for MongoDB.Bson.Path that implement jsonpath. The code is a port of the jsonpath engine in Newtonsoft.Json.

There is a newer version of this package available.
See the version list below for details.
Install-Package MongoDB.Bson.Path -Version 1.0.0
dotnet add package MongoDB.Bson.Path --version 1.0.0
<PackageReference Include="MongoDB.Bson.Path" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MongoDB.Bson.Path --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MongoDB.Bson.Path, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install MongoDB.Bson.Path as a Cake Addin
#addin nuget:?package=MongoDB.Bson.Path&version=1.0.0

// Install MongoDB.Bson.Path as a Cake Tool
#tool nuget:?package=MongoDB.Bson.Path&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ci.appveyor.com
codecov.io

MongoDB.Bson.Path

MongoDB.Bson.Path is a jsonpath implementation for MongoDB.Bson. It is a port of the jsonpath implementation from Newtonsoft.Json.

Packages

MongoDB.Bson.Path can also be installed from nuget.org.

Install with package manager:

Install-Package MongoDB.Bson.Path

or with nuget:

nuget install MongoDB.Bson.Path

Or with dotnet:

dotnet add package MongoDB.Bson.Path

Example usage

using MongoDB.Bson.Path;

void Example()
{
var o = BsonDocument.Parse(@"{
""Stores"": [
    ""Lambton Quay"",
    ""Willis Street""
],
""Manufacturers"": [
    {
    ""Name"": ""Acme Co"",
    ""Products"": [
        {
        ""Name"": ""Anvil"",
        ""Price"": 50
        }
    ]
    },
    {
    ""Name"": ""Contoso"",
    ""Products"": [
        {
        ""Name"": ""Elbow Grease"",
        ""Price"": 99.95
        },
        {
        ""Name"": ""Headlight Fluid"",
        ""Price"": 4
        }
    ]
    }
]
}");

string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co

decimal productPrice = o.SelectToken("Manufacturers[0].Products[0].Price").ToDecimal();
// 50

string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease

Assert.AreEqual("Acme Co", name);
Assert.AreEqual(50m, productPrice);
Assert.AreEqual("Elbow Grease", productName);

IList<string> storeNames = o.SelectToken("Stores").AsBsonArray.Select(s => (string)s).ToList();
// Lambton Quay
// Willis Street

IList<string> firstProductNames = o["Manufacturers"].AsBsonArray.Select(
    m => (string)m.AsBsonDocument.SelectToken("Products[1].Name")).ToList();
// null
// Headlight Fluid

decimal totalPrice = o["Manufacturers"].AsBsonArray.Aggregate(
    0M, (sum, m) => sum + m.AsBsonDocument.SelectToken("Products[0].Price").ToDecimal());
// 149.95
}

Local Development

Hacking on MongoDB.Bson.Path is easy! To quickly get started clone the repo:

git clone https://github.com/blushingpenguin/MongoDB.Bson.Path.git
cd MongoDB.Bson.Path

To compile the code and run the tests just open the solution in
Visual Studio 2019 Community Edition. To generate a code coverage report
run cover.ps1 from the solution directory.

ci.appveyor.com
codecov.io

MongoDB.Bson.Path

MongoDB.Bson.Path is a jsonpath implementation for MongoDB.Bson. It is a port of the jsonpath implementation from Newtonsoft.Json.

Packages

MongoDB.Bson.Path can also be installed from nuget.org.

Install with package manager:

Install-Package MongoDB.Bson.Path

or with nuget:

nuget install MongoDB.Bson.Path

Or with dotnet:

dotnet add package MongoDB.Bson.Path

Example usage

using MongoDB.Bson.Path;

void Example()
{
var o = BsonDocument.Parse(@"{
""Stores"": [
    ""Lambton Quay"",
    ""Willis Street""
],
""Manufacturers"": [
    {
    ""Name"": ""Acme Co"",
    ""Products"": [
        {
        ""Name"": ""Anvil"",
        ""Price"": 50
        }
    ]
    },
    {
    ""Name"": ""Contoso"",
    ""Products"": [
        {
        ""Name"": ""Elbow Grease"",
        ""Price"": 99.95
        },
        {
        ""Name"": ""Headlight Fluid"",
        ""Price"": 4
        }
    ]
    }
]
}");

string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co

decimal productPrice = o.SelectToken("Manufacturers[0].Products[0].Price").ToDecimal();
// 50

string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease

Assert.AreEqual("Acme Co", name);
Assert.AreEqual(50m, productPrice);
Assert.AreEqual("Elbow Grease", productName);

IList<string> storeNames = o.SelectToken("Stores").AsBsonArray.Select(s => (string)s).ToList();
// Lambton Quay
// Willis Street

IList<string> firstProductNames = o["Manufacturers"].AsBsonArray.Select(
    m => (string)m.AsBsonDocument.SelectToken("Products[1].Name")).ToList();
// null
// Headlight Fluid

decimal totalPrice = o["Manufacturers"].AsBsonArray.Aggregate(
    0M, (sum, m) => sum + m.AsBsonDocument.SelectToken("Products[0].Price").ToDecimal());
// 149.95
}

Local Development

Hacking on MongoDB.Bson.Path is easy! To quickly get started clone the repo:

git clone https://github.com/blushingpenguin/MongoDB.Bson.Path.git
cd MongoDB.Bson.Path

To compile the code and run the tests just open the solution in
Visual Studio 2019 Community Edition. To generate a code coverage report
run cover.ps1 from the solution directory.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.5 221 6/9/2020
1.0.4 188 6/8/2020
1.0.3 181 6/8/2020
1.0.2 209 6/7/2020
1.0.0 217 6/7/2020