Can anyone tell me why this query would return this record?
query:
db.sales.findOne({"qualified": true, created_at: {$gte:1334534400}, campaign: {$exists:1}})
record:
{
"_id" : ObjectId("4f8b6ef8da45bb3e600001fe"),
"grand_total" : 9.99,
"order_id" : "T003974723",
"items" : [
{
"price" : 9.99,
"id" : "23958754",
"name" : "UO Daisy Sunglasses",
"qty" : 1
}
],
"created_at" : 1334537675,
"visitor_id" : "1332652531389",
"unique_session_id" : "i-0f7e196e-110800",
"session_id" : "i-0f7e196e-110800",
"qualified" : true,
"needler_id" : 357,
"ip_address" : "76.103.214.29",
"in_session" : true
}
I THOUGHT I was asking mongo to give me back a record that had 'campaign' was a property. However, as you can see, this record has no property called 'campaign'
Which version of mongodb are you using? Apparently using $exists: 1 (instead of $exists: true is only supported from v1.9 on:
https://jira.mongodb.org/browse/SERVER-2322
Related
I have collection full of the following documents, for example:
{
"_id" : ObjectId("2819738917238dgd21873"),
"mailIntid" : 10000000,
"mailCreated" : "2019-02-08",
"mailLastModified" : null,
"mailReceived" : "2019-02-08",
"mailSend" : "2019-02-08",
"hadAttachment" : false,
"subject" : "nieuwe vacature ",
"bodyPreview" : "Test Body",
"importance" : "normal",
"isDeliveryRequested" : null,
"isReadReceiptRequested" : false,
"isRead" : false,
"isDraft" : false,
"inferenceClassification" : "focused",
"bodyContentType" : "html",
"senderName" : "Jobs",
"senderEmail" : "noreply#test.nl",
"fromName" : "Jobs",
"fromEmail" : "noreply#mailing.test.nl",
"flagStatus" : "notFlagged",
"urls" : "https://test.nl/request-details?id=1337",
"insertDate" : "2019-02-09",
"modifiedDate" : "2019-05-05",
"parseComplete" : true,
"rawMailUrl" : [
{
"url" : "https://test.nl/request-details?id=1337",
"parsed" : false
}
]
}
I created a index with the following code:
db.testAB.createIndex(
{"rawMailUrl.parsed": 1},
{partialFilterExpression: { "rawMailUrl.parsed": false}}
)
But whenever I use the following query, it's not using the index from above.
db.testAB.find({"rawMailUrl.parsed": false})
Any ideas? Am I doing something wrong with creating a index with arrays and a true or false expression?
You defined an index for partialFilterExpression: { "rawMailUrl.parsed": false}, i.e. all records in your index have the same value false.
An index with low number of distinct values is a bad index, it does not improve the access path. Thus the index is not used, it is just a waste of disc space.
I am trying to fetch total replies where read values for a replies is true. But I am getting count value as 3 but expected value is 2 (since only two read value is true) through Aggregation function available in Spring Data Mongo. Below is the code which I wrote:
Aggregation sumOfRepliesAgg = newAggregation(match(new Criteria().andOperator(Criteria.where("replies.repliedUserId").is(userProfileId),Criteria.where("replies.read").is(true))),
unwind("replies"), group("replies").count().as("repliesCount"),project("repliesCount"));
AggregationResults<Comments> totalRepliesCount = mongoOps.aggregate(sumOfRepliesAgg, "COMMENTS",Comments.class);
return totalRepliesCount.getMappedResults().size();
Using AND Operator inside Criteria Query and passed two criteria condition but not working as expected. Below is the sample data set:
{
"_id" : ObjectId("5c4ca7c94807e220ac5f7ec2"),
"_class" : "com.forum.api.domain.Comments",
"comment_data" : "logged by karthe99",
"totalReplies" : 2,
"replies" : [
{
"_id" : "b33a429f-b201-449b-962b-d589b7979cf0",
"content" : "dasdsa",
"createdDate" : ISODate("2019-01-26T18:33:10.674Z"),
"repliedToUser" : "#karthe99",
"repliedUserId" : "5bbc305950a1051dac1b1c96",
"read" : false
},
{
"_id" : "b886f8da-2643-4eca-9d8a-53f90777f492",
"content" : "dasda",
"createdDate" : ISODate("2019-01-26T18:33:15.461Z"),
"repliedToUser" : "#karthe50",
"repliedUserId" : "5c4bd8914807e208b8a4212b",
"read" : true
},
{
"_id" : "b56hy4rt-2343-8tgr-988a-c4f90598h492",
"content" : "dasda",
"createdDate" : ISODate("2019-01-26T18:33:15.461Z"),
"repliedToUser" : "#karthe50",
"repliedUserId" : "5c4bd8914807e208b8a4212b",
"read" : true
}
],
"last_modified_by" : "karthe99",
"last_modified_date" : ISODate("2019-01-26T18:32:41.394Z")
}
What is the mistake in the query that I wrote?
Embedded Update query works fine in mlab and atlas but not working in Cosmos DB:
My Collection structure:
{
"_id" : ObjectId("5982f3f97729be2cce108785"),
"password" : "$2y$10$F2P9ITmyKNebpoDaQ1ed4OxxMZSKmKFD9ipiU1klqio239c/nJcme",
"nin" : "123",
"login_status" : 1,
"updated_at" : ISODate("2017-05-16T09:09:03.000Z"),
"created_at" : ISODate("2017-05-16T06:08:47.000Z"),
"files" : [
{
"name" : "abc",
"updated_at" : ISODate("2017-05-16T06:08:48.000Z"),
"created_at" : ISODate("2017-05-16T06:08:48.000Z"),
"_id" : ObjectId("5982f3f97729be2cce108784")
}
],
"name" : "demo",
"email" : "email#gmail.com",
"phone" : "1231234",
}
My query is:
db.rail_zones.update(
{'_id': ObjectId("5982f3f97729be2cce108785"),
'files._id' : ObjectId("5982f3f97729be2cce108784")},
{ $set: {'files.$.name' : "Changed"}})
I get this response:
"acknowledged" : true,
"matchedCount" : 0.0,
"modifiedCount" : 0.0
According to your description, I tested this issue on my side and found the Array Update could not work as expected. I assumed that the Array Update feature has not been implemented in the MongoDB Compatibility layer of Azure CosmosDB. Moreover, I found a feedback Positional array update via '$' query support talking about the similar issue.
I have an object structure as shown below
{
"_id" : ObjectId("55d164f1c8f2c53a82535b9a"),
"plant_name" : "TOTAL",
"installed_capacity" : 3473,
"wind_data" : [
{
"date" : "16-08-15",
"timestamp" : " 16:27:15",
"generated_capacity" : 617.24,
"frequency" : 50.01
},
{
"date" : "16-08-15",
"timestamp" : " 21:21:15",
"generated_capacity" : 670.25,
"frequency" : 49.94
}, ....]
}
I need to sum up (at least retrieve) "generated_capacity" of all the objects under "wind_data" having "date" equal to "16-08-15" of "TOTAL" object. I have tried this query
db.collectionName.aggregate(
{"$unwind":"$wind_data"},
{"$match":{"plant_name":"TOTAL","wind_data.date":"16-08-15"}}
)
But, this query is not working. Please suggest some way to figure this out.
The following query would do the job
db.collectionName.aggregate([
{"$unwind":"$wind_data"},
{"$match":{"plant_name":"TOTAL","wind_data.date":"16-08-15"}},
{"$group":{"_id":"$wind_data.date","generated_capacity_sum":{"$sum":"$wind_data.generated_capacity"}}}
])
I need some ideas/tips for this. Here is a sample document I am storing:
{
"_id" : new BinData(0, "C3hBhRCZ5ZFizqbO1hxwrA=="),
"gId" : 237,
"name" : "WEATHER STATION",
"mId" : 341457,
"MAC" : "00:00:00:00:00:01",
"dt" : new Date("Fri, 24 Feb 2012 13:59:02 GMT -05:00"),
"hw" : [{
"tag" : "Weather Sensors",
"snrs" : [{
"_id" : NumberLong(7),
"sdn" : "Wind Speed"
}, {
"_id" : NumberLong(24),
"sdn" : "Wind Gust"
}, {
"_id" : NumberLong(28),
"sdn" : "Wind Direction"
}, {
"_id" : NumberLong(31),
"sdn" : "Rainfall Amount"
}, {
"_id" : NumberLong(33),
"sdn" : "Rainfall Peak Amount"
}, {
"_id" : NumberLong(38),
"sdn" : "Barometric Pressure"
}],
"_id" : 1
}]
}
What I am currently doing is using the C# driver and performing a .Save() to my collection to get upsert, however, what I want is kinda a hybrid approach I guess. Here are the distinct operations I need to be able to perform:
Upsert entire document if it does not exist
Update the dt field with a new timestamp if the document does exist
For the hw field, I need several things here. If hw._id exists, update its tag field as well as handling the snrs field by either updating existing entries so the sdn value is updated or adding entirely new entires when _id does not exist
Nothing should ever be removed from the hw array and nothing should ever be removed from the snrs array.
A standard upsert does not appear to get me what I am after, so I am looking for the best way to do what I need with as few roundtrips to the server as possible. I am thinking some of the $ Operators may be what I am needing here, but just need some thoughts on how best to approach this.
The gist of what I am doing here is keeping an accumulating, historical document of snrs entries with the immediate current value as well as retaining any historical entries in the array even though they are no longer "alive", being reported, etc. This allows future reporting on things that no longer exist in current time, but were at some point in the past. _id values are application-generated, globally unique across all documents, and never change after initial creation. For example, last week "Wind Speed" was being reported, but this week it is not. It's _id value, however, will not change if "Wind Speed" starts reporting again. Follow?
Clarifications or more detail can be provided if needed.
Thanks.
By changing the structure of your document from embedded arrays to subdocuments key'ed by the _ids you can do this.
e.g.
{
"MAC" : "00:00:00:00:00:01",
"_id" : 1,
"dt" : ISODate("2012-02-24T18:59:02Z"),
"gId" : 237,
"hw" : {
"1" : {
"snrs" : {
"1" : "Wind Speed",
"2" : "Wind Gust"
},
"tag" : "Weather Sensors"
}
},
"mId" : 341457,
"name" : "WEATHER STATION 1"
}
I created the above document by the following upsert
db.foo.update(
{_id:1},
{
$set: {
"gId" : 237,
"name" : "WEATHER STATION 1",
"mId" : 341457,
"MAC" : "00:00:00:00:00:01",
"dt" : new Date("Fri, 24 Feb 2012 13:59:02 GMT -05:00"),
"hw.1.tag" : "Weather Sensors",
"hw.1.snrs.1" : "Wind Speed",
"hw.1.snrs.2" : "Wind Gust"
}
},
true
)
Now when I run
db.foo.update(
{_id:1},
{
$set: {
"dt" : new Date(),
"hw.2.snrs.1" : "Rainfall Amount"
}
},
true
)
I get
{
"MAC" : "00:00:00:00:00:01",
"_id" : 1,
"dt" : ISODate("2012-03-07T05:14:31.881Z"),
"gId" : 237,
"hw" : {
"1" : {
"snrs" : {
"1" : "Wind Speed",
"2" : "Wind Gust"
},
"tag" : "Weather Sensors"
},
"2" : {
"snrs" : {
"1" : "Rainfall Amount"
}
}
},
"mId" : 341457,
"name" : "WEATHER STATION 1"
}