Questions tagged [mongodb-query]
This tag is for questions related to querying and updating MongoDB collections, either through the mongo shell or using a programming language driver.
mongodb-query
5,173
questions with no upvoted or accepted answers
14
votes
1
answer
962
views
MongoDB conditional aggregate query on a HABTM relationship (Mongoid, RoR)?
Rails 4.2.5, Mongoid 5.1.0
I have three models - Mailbox, Communication, and Message.
mailbox.rb
class Mailbox
include Mongoid::Document
belongs_to :user
has_many :communications
end
...
13
votes
1
answer
4k
views
How to query MongoDB aggregate with collation option
I created a "brands" collection and then added an index collation: { locale: "en", strength: 2} on the "title" field. But when I try to query it using this collation it ...
13
votes
1
answer
469
views
Meteor - publish a collection sorted on personalized score
I want to publish a collection, where documents are scored according to a combination of their fields and the context determined by the user; and the returned result is sorted based on this score, and ...
12
votes
1
answer
867
views
MongoDB - Performing an upsert on an array if arrayFilters are not satisfied
I have the following document stored in mongo:
{
"_id" : ObjectId("5d1a08d2329a3c1374f176df"),
"associateID" : "1234567",
"associatePreferences" : [
...
11
votes
1
answer
3k
views
MongoDB create index on date with specific granularity?
For example, I have a collection with this schema:
{
timestamp: new Date(some-arbitrary-specific-datetime),
some-other-field: any-type.
}
And a common query is to filter and get the documents of ...
10
votes
1
answer
3k
views
mongo Db unsupported filter type when using Any() or Contains() - Are there any workarounds / fixes?
i have seen other threads for this kind of question but none of the highlighted answers work for me.
The piece of code that throws the error is this
List<Distribution> ...
10
votes
1
answer
2k
views
find method is deprecated according to mongodb typings
I currently have this call:
const q = coll.find(query, {
tailable: true,
awaitData: true,
oplogReplay: true,
noCursorTimeout: true,
numberOfRetries: Number.MAX_VALUE
});
...
10
votes
1
answer
18k
views
MongoDB aggregation conditional adding field based on value in array
I have a collection of messages which stores the following information
code: a unique identification code of the message
from: phone number the message was sent from
to: phone number the message was ...
9
votes
0
answers
2k
views
.NET Driver with LINQ: NotSupportedException: $project or $group
The following query works:
return Database
.GetCollection<MyEntity>()
.AsQueryable()
.Where(x => x.StartDate <= instance && x.EndDate >= instance)
.GroupBy(x =&...
8
votes
0
answers
2k
views
MongoDB - Get IDs of inserted and existing documents after "Insert if not exist" operation on multiple documents
I have to insert multiple documents if they don't already exist, but the important thing is that in the query results I need to have IDs of both the inserted and already existing items.
I'm trying ...
8
votes
0
answers
239
views
MongoDB changestream notifications to only one consumer
How do we ensure that only one process will get certain Mongo collection change notifications? We run few processes which keep on listening on changes in certain collections. But all of them will get ...
8
votes
1
answer
478
views
Strongly typed query on nested field using MongoDB C# driver 2.2
Consider the following structures
public class Parent
{
public ObjectId Id { get; set; }
public IEnumerable<Child> Children { get; set; }
}
public class Child
{
public string ...
7
votes
0
answers
4k
views
Slow MongoDB lookup aggregation on very few and very small documents
There are many questions on SO about slow MongoDB aggregation for either or both of those cases:
a huge number of documents to be retrieved
very big documents
What bothers me today is that, in a ...
7
votes
0
answers
2k
views
MongoDB: Implement a read / write lock (mutex)
I need to implement some locking mechanism with MongoDB, in order to prevent inconsistent data, but allow dirty reads.
The conditions:
Acquiring a WRITE lock is only possible, if there's no READ ...
7
votes
0
answers
1k
views
Mongodb - Finding GeoNear within nested JSON objects
I need to get the closest place near a certain point using this data structure:
[
{
"data_id": "127",
"actual_data": [
{
"id": "220",
...