My mongo document is as below
{{
"startTime" : "2012-12-06T18:30:00.000Z",
"endTime" : "2012-12-07T18:30:00.000Z",
"cost" : "40",
"_id" : ObjectId("50c31b65d2dfa2271b000001")
}
{
"startTime" : "2012-12-07T18:30:00.000Z",
"endTime" : "2012-12-08T18:30:00.000Z",
"cost" : "40",
"_id" : ObjectId("50c31b65d2dfa2271b000001")
}
{
"startTime" : "2012-12-05T18:30:00.000Z",
"endTime" : "2012-12-07T18:30:00.000Z",
"cost" : "40",
"_id" : ObjectId("50c31b65d2dfa2271b000001")
}
}
I have tried to query the mongo document based on the date range,
my query is as follows:
db.collection.find({"startTime":{"$gte":"2012-12-05T18:30:00.000Z"},"endTime": {"$lt":"2012-12-12T18:30:00.000Z"}});
Response is null...
Change your query as follows and it will return the result as expected.
db.collection.find({"startTime":{"$gte":ISODate("2012-12-05T18:30:00.000Z")},"endTime": {"$lt":ISODate("2012-12-12T18:30:00.000Z")}});
Related
I have a mongoose document that is a User. Each user has arrays or words e.g. nouns. I want to be able to update the fields of the words e.g. add a +1 to the frequency or change the status number. I can access the whole array but not the individual items in the array. Also i want to be able to display the items in the array based on their fields e.g. only display those with frequency of 3 or more etc...
I have tried lots of different things but I'm just not getting it. I keep getting back error messages that findOneAndUpdate is not a function, or 'undefined' when I try to access and update documents.
{
"_id" : ObjectId("5f7d8490c1842471c6bcea42"),
"username" : "eric#mail.com",
"nouns" : [
{
"_id" : ObjectId("5f7d849cc1842471c6bcea43"),
"word" : "die Sonne",
"timeStamp" : ISODate("2020-10-07T09:04:28.436Z"),
"frequency" : 0,
"status" : 0
},
{
"_id" : ObjectId("5f7d84a8c1842471c6bcea45"),
"word" : "das Wetter",
"timeStamp" : ISODate("2020-10-07T09:04:40.940Z"),
"frequency" : 0,
"status" : 0
},
{
"_id" : ObjectId("5f7d84afc1842471c6bcea47"),
"word" : "der Apfel",
"timeStamp" : ISODate("2020-10-07T09:04:47.403Z"),
"frequency" : 0,
"status" : 0
},
{
"_id" : ObjectId("5f7d84b9c1842471c6bcea49"),
"word" : "das Maedchen",
"timeStamp" : ISODate("2020-10-07T09:04:57.388Z"),
"frequency" : 0,
"status" : 0
},
{
"_id" : ObjectId("5f7d84c8c1842471c6bcea4b"),
"word" : "das Auto",
"timeStamp" : ISODate("2020-10-07T09:05:12.036Z"),
"frequency" : 0,
"status" : 0
}
],
"verbs" : [],
"adjectives" : [],
"others" : [],
"salt" : "*****",
"hash" : "*****",
"__v" : 5
}
figured it out after hours
const ObjectId = require('mongodb').objectID
db.collections.users.updateOne(
{},
{$set: {"nouns.$[element].status": 3}},
{multi: true,
arrayFilters: [{"element._id":
ObjectId(req.body.inputId)}]}
);
I am trying to read some key-value from one object using Javascript but one key value is not reading at all. I am explaining my code below.
customer={
"_id" : ObjectId("5e21bdb921a1de5a8885c368"),
"IsActive" : true,
"GroupName" : "ashok",
"CustomerId" : "7008980097Saikantassss#okedqart",
"GroupCode" : "ashokgrp",
"GroupType" : "Group",
"GroupDescription" : "test description",
"GroupImage" : "",
"StoreCode" : "DKWF",
"GroupID" : "1673e78b-8d35-4c5b-9bda-35d8788a64ad",
"CreatedAt" : ISODate("2020-01-17T13:59:21.373Z"),
"UpdatedAt" : ISODate("2020-01-17T13:59:21.373Z"),
"__v" : 0
}
Here I have the above fetched value from mongodb but when I am printing the customerId value like below.
console.log(customer['CustomerId']);
//undefined
The above value is coming undefined but same time console.log(customer['GroupCode']); giving the value as ashokgrp. Here I need to print also customer['CustomerId'] value.
var customer={
"_id" : "5e21bdb921a1de5a8885c368",
"IsActive" : true,
"GroupName" : "ashok",
"CustomerId" : "7008980097Saikantassss#okedqart",
"GroupCode" : "ashokgrp",
"GroupType" : "Group",
"GroupDescription" : "test description",
"GroupImage" : "",
"StoreCode" : "DKWF",
"GroupID" : "1673e78b-8d35-4c5b-9bda-35d8788a64ad",
"CreatedAt" : "2020-01-17T13:59:21.373Z",
"UpdatedAt" : "2020-01-17T13:59:21.373Z",
"__v" : 0
}
console.log(customer.CustomerId);
It Will work .
I have a collection that consists of posts. Inside those post objects are properties about the post, one of which being an array of tags associated with the post. Inside the array of tags are more objects, each of which are the tags with a name property. I would like to get back all the posts from this collection which have a given tag in them based on the name of the tag.
Here is an example of a post object within the collection:
{
"_id" : ObjectId("5c6a0478cba09c148b497fc6"),
"tags" : [
{
"_id" : "5c69d974e05511106048780e",
"name" : "Food",
"text_color" : "#ffffff",
"bg_color" : "#02569b",
"createdAt" : "2019-02-17T22:00:20.143Z",
"updatedAt" : "2019-02-17T22:00:20.143Z",
"__v" : 0
},
{
"_id" : "5c69d95de05511106048780d",
"name" : "Drinks",
"text_color" : "#ffffff",
"bg_color" : "#0175c2",
"createdAt" : "2019-02-17T21:59:57.758Z",
"updatedAt" : "2019-02-17T21:59:57.758Z",
"__v" : 0
}
],
"title" : "Title of the post",
"body" : "body of the post",
"author_id" : ObjectId("5c5e0d3b647f12e949cbea1e"),
"author_name" : "garrett",
"likes_count" : 1,
"createdAt" : ISODate("2019-02-18T01:03:52.497Z"),
"updatedAt" : ISODate("2019-02-28T00:25:21.969Z"),
"__v" : 0,
"dislikes_count" : 0
}
Of course I have other post objects in this collection, some of which may have different tags. How can I get all posts with the food tag to be returned?
A basic find query will do the job here. Here's how it looks
const tagName = 'food;
db.posts.find({ 'tags.name': tagName }).then(console.log);
My current doc is like this
{
"_id" : ObjectId("55ece69df332eb0000d34e12"),
"parent" : "P1",
"hierarchy" : {},
"hidden" : false,
"type" : "xyz",
"name" : "Mike",
"code" : "M110",
"date" : ISODate("2015-09-07T01:21:33.965Z"),
"__v" : 3,
"job_id" : "ca50fdf0-6904-11e6-b9af-1b0ea5d7f792"
}
now i want to have job_id as array instead. So i changed the field type to array in model schema. Now when I try to update the existing docs with some changes.
Document before saving looks like this
{
"_id" : ObjectId("55ece69df332eb0000d34e12"),
"parent" : "P1",
"hierarchy" : {},
"hidden" : false,
"type" : "xyz",
"name" : "Mike",
"code" : "M110",
"date" : ISODate("2015-09-07T01:21:33.965Z"),
"__v" : 3,
"job_id" : ["ca50fdf0-6904-11e6-b9af-1b0ea5d7f792",
"f1f04a95-42fa-41e5-a2c6-89c52c9c63f2"
]
}
so when i call model.save() method i'm getting following error:
{ [MongoError: The field 'job_id' must be an array but is of type String in document {_id: ObjectId('55ece69df332eb0000d34e12')}]
name: 'MongoError',
code: 16837,
err: 'The field \'job_id\' must be an array but is of type String in document {_id: ObjectId(\'55ece69df332eb0000d34e12\')}' }
What is the best way to handle this??
Let's say my dataset is as follows AFTER completing a lot of aggregation magic on it:
{ "postid" : "5700edfe03fcdb000347bebf", "comms" : { "commenter" : "56f3f70d4de8c74a69d1d5e1", "id" : ObjectId("570175e6c002e46edb922ae6")}, "flag" : "M"}
{ "postid" : "5700edfe03fcdb000347bebf", "comms" : { "commenter" : "56f3f70d4de8c74a69d1d5e1", "id" : ObjectId("570175e6c002e46edb922ae5")}, "flag" : "M"}
{ "postid" : "5700edfb03fcdb000347bebe", "comms" : { "commenter" : "56f3f70d4de8c74a69d1d5e1", "id" : ObjectId("570176a3c002e46edb922ae7")}, "flag" : "F"}
{ "postid" : "5700edfb03fcdb000347bebe", "comms" : { "commenter" : "56f3f70d4de8c74a69d1d5e1", "id" : ObjectId("570176a3c002e46edb922ae6")}, "flag" : "M"}
{ "postid" : "5700edfb03fcdb000347bebe", "comms" : { "commenter" : "56f3f70d4de8c74a69d1d5e1", "id" : ObjectId("570176a3c002e46edb922ae5")}, "flag" : "G"}
Each line represents a post and then comment that is linked to the post sorted by time, with the latest comment in a post at the top and the oldest ones at the bottom. You can see this order by looking at the descending values of comms.id
Now I want to extract the data in an aggregation query so it looks like this. The data below represents the latest comment in all the posts returned:
{ "_id" : "5700edfb03fcdb000347bebe", "maxcom" : ObjectId("570176a3c002e46edb922ae7"), "flag" : "F"}
{ "_id" : "5700edfe03fcdb000347bebf", "maxcom" : ObjectId("570175e6c002e46edb922ae6"), "flag" : "M"}
The closest I have gotten to getting my result is running the query below:
group3={"$group": {
"_id" : "$postid",
"maxcom" : {
"$max" : "$comms.id"
}
}};
The code above returns this:
{ "_id" : "5700edfb03fcdb000347bebe", "maxcom" : ObjectId("570176a3c002e46edb922ae7") }
{ "_id" : "5700edfe03fcdb000347bebf", "maxcom" : ObjectId("570175e6c002e46edb922ae6") }
How do I return the output I require that shows the "flag" field as well?