var doc = new Document<dynamic>
{
Id = "uniqueId",
Content = new
{
fruit,
robots,
}
};
__Bucket.Insert(docKey, doc);
}
let’s say you have something like that. The document Id has to be unique inside the document and the key(dockey) has to be unique per document? I can think of document Id as a “type” field and key(often refereed to as document IDs) as the unique identifier for the document?
@krnforsale
The Id inside the Document classes is actually the same thing as the Key in the older-style API calls. There’s just been a naming convention change.
If you’re using Document, use it like this:
var doc = new Document<dynamic>
{
Id = "uniqueId",
Content = new
{
fruit,
robots,
}
};
__Bucket.Insert(doc);
To use the older API methods that take a key, use them like this:
var doc = new
{
fruit,
robots,
};
__Bucket.Insert("uniqueId", doc);
@btburnett3
var doc = new Document<dynamic>
{
Id = "uniqueId",
Content = new
{
type = "typeValue",
fruit,
robots,
}
};
__Bucket.Insert(doc);
if I have something like that above and I use n1ql query like this, “SELECT content FROM BucketName
WHERE type = ‘typeValue’” I’m getting a “cannot perform runtime binding on a null reference” error. What is wrong with my query?
edit- “SELECT * FROM BucketName
WHERE type = ‘typeValue’” will return me the values I need but when I use
var result = dbHelper.__Bucket.Query<dynamic>(queryRequest);
foreach (var row in result.Rows)
{
var beacon = new Beacon()
{
SerialNumber = row.serialNumber, //originally row.content.serialNumber
ReceivedDate = Convert.ToDateTime(row.receivedDate),
ReceiverId = row.receiverId,
Distance = Convert.ToDouble(row.distance),
Rssi = Convert.ToInt32(row.rssi),
NewDistance = Convert.ToDouble(row.newDistance),
DistanceTesting = Convert.ToDouble(row.distanceTesting),
};
_List.Add(beacon);
}
ViewBag.Beacon = _beaconsList;
it doesn’t return my anything.
@krnforsale
Change your query to this:
SELECT BucketName.* FROM BucketName WHERE type = 'typeValue'
Without the leading BucketName.
in the select projection the query result will be nested within a property that is the name of the bucket. This is slightly different than traditional SQL but is useful when dealing with joins in N1QL. But it will trip you up if you don’t know about.
Brant
2 Likes
@btburnett3 That works, thank you so much. I had another question regarding the efficiency of the my document design. I am putting each object into the document with a unique ID and inserting them into a bucket. If I use primary index, it will store the key value pair in the RAM. This way, when I query it, it will be faster. Is the way I’m approaching this sound fine?
@krnforsale
Generally speaking, using primary indexes for your queries is actually very slow, at least once you’re up to scale. It requires it to go through every document in the bucket looking for matches.
At a minimum, I usually recommend creating an index on the type
attribute. Then any query with a WHERE type = 'x'
predicate will at least only be scanning through all documents of that type. If you only have a handful of documents of that type that’s usually sufficient (the equivalent of a primary key scan on a SQL table in RDBMS).
You’ll just have to look at your models, especially how many documents are involved, to find the best indexing approach.
Brant
3 Likes