I would like to query a collection in a MongoDB database to find all records that contain a portion of an ObjectID. For a normal string I can use a regex like this:
db.teams.find({"some_string": /^51eed/})
But how would I do something similar on an ObjectID?
Specifically, I have a collection that looks something like this:
{ "status" : 0, "_id" : ObjectId("51e97ff613e737801d000002") }
{ "status" : 0, "_id" : ObjectId("51ee7513d1f7c57420000002") }
{ "status" : 0, "_id" : ObjectId("51eed9dd5b605af404000002") }
{ "status" : 0, "_id" : ObjectId("51eedab39108d8101c000002") }
I would like to query (in mongo) for all records where the ObjectId starts with "51eed". Your help is greatly appreciated.
You can simply do this with a range search, start at "51eed0000000000000000000" and end at "51eee0000000000000000000" (notice the "d" -> "e"):
db.teams.find( {
_id: {
$gte: ObjectId("51eed0000000000000000000"),
$lt: ObjectId("51eee0000000000000000000"),
}
} )
You could possibly just put that into a new ObjectId and fill the rest with 0's like:
db.teams.find({_id:{$gte:ObjectId("51eed0000000000000000000")}})
Should do the trick.
Related
I'm evaluating the following query on my collection with fake data:
db.test_result.find({"date": {$gte: ISODate("2021-07-27"), $lte: ISODate("2021-08-31")}}).count()
And despite the fact that I use $lte it does not include the second date value. Is it a bug? If so then how do I make the query so that the left and right borders are included?
Here is what a fake json obj looks like:
{
"nfl": "Some",
"rStatus": false,
"mac": "02:00:00:00:00:00",
"date": "2021-07-27T12:17:57",
"MDCode": "123132132123",
}
With this given input:
{
"_id" : ObjectId("596c6fea53cc7100104628fa"),
"timestamp" : ISODate("2017-07-17T08:06:02.041Z"),
}
{
"_id" : ObjectId("596c7162973f33000fc8bb81"),
"timestamp" : ISODate("2017-07-17T08:12:18.170Z")
}
{
"_id" : ObjectId("596c736c15371f00106b9e3a"),
"timestamp" : ISODate("2017-07-17T08:21:00.291Z")
}
This query:
...find({"timestamp": {$gte: ISODate("2017-07-17T08:06:02.041Z"), $lte: ISODate("2017-07-17T08:12:18.170Z")}})
Would return:
{
"_id" : ObjectId("596c6fea53cc7100104628fa")
"timestamp" : ISODate("2017-07-17T08:06:02.041Z")
}
{
"_id" : ObjectId("596c7162973f33000fc8bb81"),
"timestamp" : ISODate("2017-07-17T08:12:18.170Z")
}
Which is what you would expect, the results match to the dot the 2 borders. So in a nutshell it would be included if it is an exact match, otherwise you would get only the once in between.
I'm new to mongo and am trying to do a very simple query in this collection:
{
"_id" : ObjectId("gdrgrdgrdgdr"),
"administrators" : {
"-HGFsfes" : {
"name" : "Jose",
"phone" : NumberLong(124324)
},
"-HGFsfqs" : {
"name" : "Peter",
"phone" : "+43242342"
}
},
"countries" : {
"-dgfgrdg : {
"lang" : "en",
"name" : "Canada"
},
"-grdgrdg" : {
"lang" : "en",
"name" : "USA"
}
}
}
How do I make a query that returns the results of administrators with name like "%Jos%" for example.
What I did until now is this: db.getCollection('coll').find({ "administrators.name": /Jos/});
And variations of this. But every thing I tried returns zero results.
What am I doing wrong?
Thanks in advance!
Your mistake is that administrators is not an array, but an object with fields that are themselves objects with name field. Right query will be
{ "administrators.-HGFsfes.name": /Jos/}
Unfortunatelly this way you're only querying -HGFsfes name field, not other administrator name field.
To achieve what you want, the only thing to do is to replace administrators object by an array, so your document will look like this :
{
"administrators" : [
{
"id" : "-HGFsfes",
"name" : "Jose",
"phone" : 124324
},
{
"id" : "-HGFsfqs",
"name" : "Peter",
"phone" : "+43242342"
}
],
countries : ...
}
This way your query will work.
BUT it will return documents where at least one entry in administrators array has the matching name field. To return only administrator matching element, and not whole document, check this question and my answer for unwind/match/group aggregation pipeline.
You need to use query like this:
db.collection_name.find({})
So if your collection name is coll, then it would be:
db.coll.find({"administrators.-HGFsfes.name": /Jos/});
Look this for like query in mongo.
Also, try with regex pattern like this:
db.coll.find({"administrators..-HGFsfes.name": {"$regex":"Jos", "$options":"i"}}});
It will give you only one result because your data is not an array as below in screenshot:
If you want multiple results, then you need to restructure your data.
Ok, think i've found a better solution for you, with aggregation framework.
Run the following query on your current collection, will return you all administrators with name "LIKE" jos (case insensitive with i option) :
db.test1.aggregate(
[
{
$project: {
administrators:{ $objectToArray: "$administrators"}
}
},
{
$unwind: {
path : "$administrators"
}
},
{
$replaceRoot: {
newRoot:"$administrators"
}
},
{
$match: {
"v.name":/jos/i
}
},
]
);
Output
{
"k" : "-HGFsfes",
"v" : {
"name" : "Jose",
"phone" : NumberLong(124324)
}
}
"k" and "v" are coming from "$objectToArray" operator, you can add a $project stage to rename them (or discard if k value doesn't matter)
Not sure for Robomongo testing but in Studio 3T, formerly Robomongo, you can either copy/paste this query in Intellishell console, or copy/import in aggregation tab, (small icon 'paste from the clipboard').
Hope it helps.
Since every collection in mongodb has a default index on the _id column, I wanted to leverage it for my scenario as below.
I have my collection as below,
{
"_id":{
"timestamp" : ISODate("2016-08-24T23:22:20.201Z"),
"departmentname" : "sales",
"city":"NJ"
}
//Other fields in my collection
}
With this structure I am able to query as below,
db.getCollection('test').find(
{
"_id" : {
"timestamp" : ISODate("2016-08-21T23:22:20.201Z"),
"departmentname" : "sales",
"city":"NJ"
}
}
)
But, when I query by one or more fields in which are part of _id column as below,
db.getCollection('test').find(
{
"_id" : {
"timestamp" : ISODate("2016-08-21T23:22:20.201Z")
}
}
)
(OR)
db.getCollection('test').find(
{
"_id" : {
"departmentname" : "sales"
}
}
)
(OR)
db.getCollection('test').find(
{
"_id" : {
"departmentname" : "sales",
"city":"NJ"
}
}
)
I do not see any documents returned
When I checked with .explain() I see that it has used Index but did not find any documents.
Also, I would like to do date range queries on timestamp field along with query on one or more fields in the _id column like below,
db.getCollection('test').find(
{
"_id.timestamp" : {
"$gte": ISODate("2011-08-21T23:22:20.201Z")
},
"_id.departmentname" : "sales"
}
)
But, I do not see any documents returned. When I run .explain() I see it has used colscan and not index.
Can someone help me on the right way to query by one or more fields on my _id column.
Thanks,
Sri
You can try following query, in first case:-
db.getCollection('test').find(
{
"_id.timestamp" : ISODate("2016-08-21T23:22:20.201Z")
})
And this for multiple fields:
db.getCollection('test').find(
{
"_id.timestamp" : ISODate("2016-08-21T23:22:20.201Z"),
"_id.departmentname" : "sales",
})
I need to search all records that match a date condition. On MongoDB I've a bunch of data like this:
{
"_id" : "9ed3b937-0f43-4613-bd58-cb739a8c5bf6",
"userModels" : {
"5080" : {
"generated_date_timestamp" : NumberLong(1413382499442),
"model_id" : 5080,
},
}
"values" : {}
}
I'm not able to convert that NumberLong in something that can be used by this query:
db.anonProfile.find({
"userModels.5080.generated_date_timestamp" : { "$gte" : ISODate("2013-10-01T00:00:00.000Z") }
});
"_id" has been saved as String so I cannot use for a ObjectID search. [btw, is it possible to do?]
Any clue?
Tnx, Andrea
You can query by
db.anonProfile.find({
"userModels.5080.generated_date_timestamp" : { "$gte" : ISODate("2013-10-01T00:00:00.000Z").getTime() }
});
I have a MongoDB document like this example doc:
{
"_id" : "A",
"articleNumber" : "0123456",
"shopDependentProperties" :
{
"shop" : "DE",
"foo" : "foo",
"bar" : "bar"
}
}
and want to pull out the properties of shopDependentProperties, to have the following result
{
"_id" : "A",
"articleNumber" : "0123456",
"foo" : "foo",
"bar" : "bar"
}
In MongoDB Shell i can solve it this way:
db.test.aggregate(
[
{
$project:
{
_id : "$_id",
articleNumber : "$articleNumber",
foo:"$shopDependentProperties.foo",
bar:"$shopDependentProperties.bar"
}
}
]
)
But: In Spring Data MongoDB i can't extract the embedded document contents.
I tried many combinations, nothing worked. For example:
ProjectionOperation projection = Aggregation.project("_id");
projection.andExpression("shopDependentProperties.foo").as("foo");
projection.andExpression("shopDependentProperties.bar").as("bar");
System.out.println(projection.toDBObject(Aggregation.DEFAULT_CONTEXT));
will ignore the shopDependentProperties.shop stuff and just print out
{ "$project" : { "_id" : 1}}
Any suggestions?
Thx
Haven't tested this, but as of
http://docs.mongodb.org/manual/reference/operator/aggregation/project/
you specify included / excluded fields like this:
db.test.aggregate(
[
{
$project:
{
_id : "$_id",
articleNumber : 1,
"shopDependentProperties.foo": 1,
"shopDependentProperties.bar": 1
}
}
]
)
Further down they explain, how to include embedded documents in the projection result.
I know how to do it in MongoDB, the problem was about doing the same thing in Spring Data.
But it works the same way, why didn't I try that before?
Solution:
ProjectionOperation projection = Aggregation.project(
"brandName",
"$shopDependentProperties.foo",
"$shopDependentProperties.bar" );