I want to apply skip and limit for paging in nested array of a document how can I perform this [Efficient Way]
My Document recored like
{
"_id":"",
"name":"",
"ObjectArray":[{
"url":"",
"value":""
}]
}
I want to retrieve multiple document and every document contain 'n' number of record.
I am using $in in find query to retrieve multiple record on basis of _id but how can i get certain number of element of ObjectArray in every document?
You can try like this -
db.collection.find({}, {ObjectArray:{$slice:[0, 3]}})
This will provide you records from 0..3
$slice:[SKIP_VALUE, LIMIT_VALUE]}
For your example:-
db.collection.find({"_id":""}, {ObjectArray:{$slice:[0, 3]}})
Here is the reference for MongoDB Slice feature.
http://docs.mongodb.org/manual/reference/operator/projection/slice/
Related
I've a document in ArangoDB.
{ title: "title 12345", tags : ["tagx", "tagy", "tagz"}
I've an full text index on title and another hash index on tags[*].
I've a query where I want to use full text search on title and apply some filtering via tags and get skip x limit 10 in results. I'm able to achieve this. This will help me in pagination. In one API call, I can return the user 10 items.
However, I also want to get all the unique tags which are present in the result(without skip and limit constraint) without hitting all the documents present in the result. This will help me to show the tags which user can further select to narrow down the search.
We can assume that there will be a small number of unique tags(around 30-40) in the database. Is there an efficient way to achieve this in ArangoDB? Maybe, we can create some new indexes or change the schema to achieve this.
Assuming that you are going to create a collection with documents named test with the following content
Just to be reproducible, the data that I am inserting is:
[{"_key":"342","_id":"test/342","_rev":"504","tags":["tagx","tagy","tagz"],"title":"title 12345"},{"_key":"564","_id":"test/564","_rev":"591","tags":["tagx","tagy","tagt"],"title":"title another"},{"_key":"510","_id":"test/510","_rev":"538","tags":["tagh","tagk","tagz"],"title":"title 56789"}]
If you run the following AQL query
let tags = (for i in test
return i.tags)[**]
return unique(tags)
This will return
[
[
"tagk",
"tagh",
"tagt",
"tagz",
"tagy",
"tagx"
]
]
As you can see there are different tags repeated across multiple documents and the results are showing an array with just the unique ones
I'm trying to create an interface which gives our team the ability to build relatively simple queries to segment customers and aggregate data about those customers. In addition to this, I want to build a "List" feature" that allows users to upload a CSV that can be separately generated for more complex one-off queries. In this scenario, I'm trying to figure out the best way to query the database.
My main question is how the $in operator works. The example below has an aggregate which tries to check if a primary key (object ID) is in an array of object IDs.
How well does the $in operator perform? I'm mostly wondering how this query will run – does it loop over the array and look for documents that match each value in the array for N lookups, or will it loop over all of the documents in the database and for each one, loop over the array and check if the field matches?
db.getCollection('customers').aggregate([
{
$match: {
'_id': { $in: ['ObjectId("idstring1")','ObjectId("idstring2")'...'ObjectId("idstring5000")']}
}
}
])
If that's not how it works, what's the most efficient way of aggregating a set of documents given a bunch of object IDs? Should I just do the N lookups manually and pipe the documents into the aggregation?
My main question is how the $in operator works. The example below has
an aggregate which tries to check if a primary key (object ID) is in
an array of object IDs.
Consider the code:
var OBJ_ARR = [ ObjectId("5df9b50e7b7941c4273a5369"), ObjectId("5df9b50f7b7941c4273a5525"), ObjectId("5df9b50e7b7941c4273a515f"), ObjectId("5df9b50e7b7941c4273a51ba") ]
db.test.aggregate( [
{
$match: {
_id: { $in: OBJ_ARR }
}
}
])
The query tries to match each of the array elements with the documents in the collection. Since, there are four elements in the OBJ_ARR there might be four documents returned or lesser depending upon the matches.
If you have N _id's in the lookup array, the operation will try to find match for all elements in the input array. The more number of values you have in the array, the more time it takes; the number of ObjectIds matter in query performance. In case you have a single element in the array, it is considered as one equal match.
From the documentation - the $in works like an $or operator with equality checks.
... what's the most efficient way of aggregating a set of documents
given a bunch of object IDs?
The _id field of the collection has a unique index, by default. The query, you are trying will use this index to match the documents. Running an explain() on a query (with a small set of test data) confirms that there is an index scan (IXSCAN) on the match operation with $in used with the aggregation query. That is a better performing query (as it is) becuse of the index usage. But, the aggregation query's later/following stages, size of the data set, the input array size and other factors will influence the overall performance and efficiency.
Also, see:
Pipeline Operators and Indexes and Aggregation Pipeline Optimization.
I am studying Firestore with React.
After I added the array data into the firestore, I am trying to count the certain element in array of data in the document.
Like the picture I attached, there are several documents includes array data.
I want to count the number of CS 34800 in class array from all documents.
What is the best way to get it?
Thank you!
enter image description here
There are no counting or aggregating operations on Firebase, so that way you have it now, you will have to query for all the documents that match your criteria, then count the number of documents in your result set.
You can use an "array_contains" operator to get all the documents where an array contains some value.
collectionRef.where("class", "array-contains", "CS 34800").get()
.then(querySnapshot => {
const count = querySnapshot.size
});
If you need to get a count without making a query for documents, you'll have to maintain that yourself for every string that you could possibly need to count, which is an entirely different problem.
I have been searching through the MongoDB query syntax with various combinations of terms to see if I can find the right syntax for the type of query I want to create.
We have a collection containing documents with an array field. This array field contains ids of items associated with the document.
I want to be able to check if an item has been associated more than once. If it has then more than one document will have the id element present in its array field.
I don't know in advance the id(s) to check for as I don't know which items are associated more than once. I am trying to detect this. It would be comparatively straightforward to query for all documents with a specific value in their array field.
What I need is some query that can return all the documents where one of the elements of its array field is also present in the array field of a different document.
I don't know how to do this. In SQL it might have been possible with subqueries. In Mongo Query Language I don't know how to do this or even if it can be done.
You can use $lookup to self join the rows and output the document when there is a match and $project with exclusion to drop the joined field in 3.6 mongo version.
$push with [] array non equality match to output document where there is matching document.
db.col.aggregate([
{"$unwind":"$array"},
{"$lookup":{
"from":col,
"localField":"array",
"foreignField":"array",
"as":"jarray"
}},
{"$group":{
"_id":"$_id",
"fieldOne":{"$first":"$fieldOne"},
... other fields
"jarray":{"$push":"$jarray"}
}},
{"$match":{"jarray":{"$ne":[]}}},
{"$project":{"jarray":0}}
])
I have documents in a collection that have an array of properties (1-400 different numeric values).
Now i want to get all documents of another collection that have one of these properties (these documents only have one property).
How can i do that, preferably in one call?
As MongoDB is no relational DBMS this isn't possible to achieve with only one call.
What you need to do is to first retrieve your document your want to use for your search. Upon you retrieved it, you're using that array stored in the document todo a $in query on the field for the other collection. So for the mongo shell this could be something like this:
var ar = db.coll1.findOne().numArray
db.coll2.find({b: { $in : ar }})