Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

using System.Text.Json; using System.Text.Json.Nodes; using QuipQuestDB; var doc1Content…

  1. User

    using System.Text.Json; using System.Text.Json.Nodes; using QuipQuestDB; var doc1Content = JsonNode.Parse(@"{""name"": ""Alice"", ""age"": 30, ""city"": ""Wonderland""}"); var doc2Content = JsonNode.Parse(@"{""name"": ""Bob"", ""age"": 25, ""city"": ""Builderland""}"); var doc3Content = JsonNode.Parse(@"{""name"": ""Charlie"", ""age"": 35, ""city"": ""Chocolate Factory""}"); var query = JsonNode.Parse(@"{""age"": 30}");

    var database = new Database("shiza"); var collection = database.AddCollection("skibida"); var index = collection.AddDocument(doc1Content); // collection.UpdateDocument(index, "{"nickname": "Not_A_Skibida", "age": 29}"); var document = collection.GetDocument(index);

    Console.WriteLine(document); collection.GetAllDocuments().ForEach(Console.WriteLine);

    collection.Find(query); using System.Text.Json; using System.Text.Json.Nodes; using NanoidDotNet;

    namespace QuipQuestDB;

    public class Document { public JsonNode Content { get; set; } public DateTime CreatedAt { get; set; }

    public Document(JsonNode content)
    {
        Content = content;
        CreatedAt = DateTime.UtcNow;
    }
    
    public override string ToString()
    {
        return JsonSerializer.Serialize(this);
    }
    

    }

    public class Collection { public string Name { get; set; } private Dictionary<string, Document> Documents { get; set; } = [];

    public List<Document> Find(JsonNode query)
    {
        var list = Documents.Values
            .Where(document => MatchValue(document.Content, query))
            .ToList();
    
        return list;
    }
    
    private bool MatchValue(JsonNode document, JsonNode query)
    {
        if (query is JsonObject jsonQueryObject)
        {
            if (document is JsonObject jsonDocumentObject)
            {
                foreach (var kvp in jsonQueryObject)
                {
                    if (jsonDocumentObject.ContainsKey(kvp.Key)
                        && jsonDocumentObject.TryGetPropertyValue(kvp.Key, out var value)
                        && value != null
                        && kvp.Value.Equals(value.AsValue()))
                    {
                        return true;
                    }
                }
            }
        }
    
        return false;
    }
    
    public string AddDocument(JsonNode content)
    {
        var index = Nanoid.Generate(size: 10);
        var newDocument = new Document(content);
        Documents.Add(index, newDocument);
        return index;
    }
    
    public Document GetDocument(string index)
    {
        Documents.TryGetValue(index, out var document);
        return document ?? Documents.Values.First();
    }
    
    public List<Document> GetAllDocuments() => Documents.Select(x => x.Value).ToList();
    public IEnumerable<Document> GetAllDocumentsIterator() => Documents.Select(x => x.Value).AsEnumerable();
    public Document GetDocumentByIndex(string index) => GetDocument(index);
    
    public void UpdateDocument(string index, JsonNode content)
    {
        GetDocument(index).Content = content;
    }
    
    public Document RemoveDocument(string index)
    {
        var document = GetDocument(index);
        Documents.Remove(index);
        return document;
    }
    
    public Collection(string name)
    {
        Name = name;
    }
    

    }

    public class Database { public string Name { get; set; } private Dictionary<string, Collection> Collections { get; set; } = [];

    public Database(string name)
    {
        Name = name;
    }
    
    public Collection AddCollection(string name)
    {
        var newCollection = new Collection(name);
        Collections.Add(name, newCollection);
        return newCollection;
    }
    
    public Collection GetCollection(string name)
    {
        Collections.TryGetValue(name, out var collection);
        return collection ?? Collections.First().Value;
    }
    
    public List<Collection> GetAllCollections() => Collections.Values.ToList();
    
    public IEnumerable<Collection> GetAllCollectionsIterator() => Collections.Values.AsEnumerable();
    
    public Collection RemoveCollection(string name)
    {
        var collection = GetCollection(name);
        Collections.Remove(name);
        return collection;
    }
    

    }

    Why it can't find value, if it's obviously here?

  2. Assistant

    The issue you're encountering is due to the way you're comparing JsonNode objects in your MatchValue method. When you use kvp.Value.Equals(value.AsValue()), you're comparing the JsonNode instances themselves, not the actual underlying values. Since JsonNode's Equals method doesn't override the default reference equality, even if two JsonNode instances contain the same value, they are considered unequal unless they reference the same object.

    To fix this, you need to extract the actual values from the JsonNode instances and compare those values. Here's how you can modify your MatchValue method:

    private bool MatchValue(JsonNode document, JsonNode query)
    {
        if (query is JsonObject jsonQueryObject && document is JsonObject jsonDocumentObject)
        {
            foreach (var kvp in jsonQueryObject)
            {
                if (jsonDocumentObject.TryGetPropertyValue(kvp.Key, out var valueNode)
                    && valueNode != null && kvp.Value != null)
                {
                    var queryValue = kvp.Value.GetValue<object>();
                    var documentValue = valueNode.GetValue<object>();
    
                    if (queryValue.Equals(documentValue))
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    

    Explanation:

    • Extract Values: Use GetValue<object>() to extract the underlying values from the JsonNode instances. This method retrieves the value stored in the JsonNode, regardless of its JSON type (string, number, boolean, etc.).

    • Compare Values: After extracting the values, use Equals to compare them. This ensures that you're comparing the actual data (e.g., the integer 30), not the JsonNode object references.

    With this modification, your Find method will correctly identify documents where the values match the query, even if they're represented as different JsonNode instances.

    Updated Find Method Usage:

    Now, when you run:

    var query = JsonNode.Parse(@"{""age"": 30}");
    var matchingDocuments = collection.Find(query);
    matchingDocuments.ForEach(Console.WriteLine);
    

    It will correctly find and display the document(s) where "age": 30.

    Additional Notes:

    • Null Checks: Ensure that both kvp.Value and valueNode are not null before attempting to extract their values.

    • Type Safety: Since GetValue<object>() returns object, it's type-safe and can handle different JSON value types.

    Key Takeaway:

    When working with JsonNode (or similar tree-like data structures), be cautious about comparing nodes directly. Always extract and compare the underlying values to ensure accurate equality checks.