0

I am trying to traverse into a MongoDB Collection of the form :

{"_id":"lkashfhasdfhsdlafhlkjsdahf",
"
"Array":[{

                    "array_1":"17:00"}],

}

I want to get the array_1 in the document above I tried it using the following code in C#

result = Database.CollectionName.AsQueryable().Where(r => r.Array.array== array_inpit(Input) && condition2).ToList();

Expected Result : All the documents with the matching Array

Current Output : Error

Any help how should i proceed with this.

2 Answers 2

1

Basic example using the MongoDb.Driver package. You'll need some data types defined, something like:

// Use [BsonIgnoreExtraElements] attribute when not defining ALL fields in record
internal class MainRecord
{
    public ObjectId _id { get; set; }

    public List<ArrayItem> ResourceStatus { get; set; }

    // All the other fields here
}

// [BsonIgnoreExtraElements] as above
internal class ArrayItem
{
    public string E2EId { get; set; }
}

(Note - I've omitted all the fields not required to do the array search).

Then to actually query the data:

var client = new MongoClient();

IMongoDatabase db = client.GetDatabase("database-name-here");

var collectionName = "collection-name-here";

IMongoCollection<MainRecord> collection = db.GetCollection<MainRecord>(collectionName);

var filter = Builders<MainRecord>.Filter.ElemMatch(x => x.ResourceStatus, x => x.E2EId == "1fdsfsfsfsfsffds0");

var result = collection.Find(filter);

EDIT: And I'd recommend looking at this post which gives some alternative approaches.

Sign up to request clarification or add additional context in comments.

Comments

0

I create simple classes do demonstrate how to:

MongoDB Class

public class MongoDBConnect : IDisposable
{
    public IMongoClient client;
    public IMongoDatabase database;

    public MongoDBConnect()
    {
        client = new MongoClient("mongodb://localhost");
        database = client.GetDatabase("dbo");
    }

    public void Dispose()
    {
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }
}

Your Collection Class

public class YourCollection
{
    [BsonId()]        
    public ObjectId Id { get; set; }

    [BsonElement("YourCollectionID")]
    public string YourCollectionID { get; set; }

    [BsonElement("AccessKey")]
    public string AccessKey { get; set; }
}

Your Collection Data Class

public class YourCollectionDAO : MongoDBConnect
{
    public YourCollectionDAO()
    {

    }

    public YourCollection Find(string yourCollectionID)
    {
        var collection = this.database.GetCollection<User>("YourCollection");

        Expression<Func<YourCollection, bool>> filter = x => x.yourCollectionID == yourCollectionID;

        IList<YourCollection> filtering = collection.Find(filter).ToList();

        var yourCollectionItem = filtering.Where(x => x.yourCollectionID == yourCollectionID).FirstOrDefault();

        return yourCollectionItem;
    }
}

Hope it helps.

Comments

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.