I have written SOQL query to get the data between 2 dates 2020-06-01 and 2020-06-30
SELECT Id ,LastModifiedDate
FROM Account
WHERE LastModifiedDate >= 2020-06-01T00:00:00Z
AND LastModifiedDate <= 2020-06-30T23:59:59Z
but I want to implement it using method chain
See this link for information on how to convert a string representation of a SF date to a date literal.
The rest is just a modification for the official docs per your query.
(async _ => {
const results = await conn.sobject("Account")
.find(
// conditions in JSON object
{
LastModifiedDate : { $gte : jsforce.SfDate.toDateTimeLiteral('2020-06-01T00:00:00Z'), $gte : jsforce.SfDate.toDateTimeLiteral('2020-06-30T23:59:59Z') },
},
// fields in JSON object
{
Id: 1,
LastModifiedDate: 1
}
) // end method chain
console.log(results)
})()
Related
In mongodb I'm wondering how you can properly insert json to the db from the command line with an object that contains an object?
db.burgers.insert(
[
{'burger': {
'patty': 'beef',
'cheese': false,
'toppings': [
'ketchup', 'onions', 'pickles'
]
}}
]
)
When I try the code above, db.burgers.burger.find() returns 0, when I am expecting it to return 1
The following is how they get returned.
db.burgers.find().pretty()
{
"_id" : ObjectId("5f87b58afd426d861d55b5ff"),
"burger" : {
"patty" : "beef",
"cheese" : false,
"toppings" : [
"ketchup",
"onions",
"pickles"
]
}
}
In mongodb I'm wondering how you can properly insert json to the db
from the command line with an object that contains an object?
Your inserting the document into the collection is correct. You can use insertOne, insertMany or insert methods to insert documents. The insertOne is specific for insert one document at a time. insert and insertMany can be used to insert one or more documents at a time.
From the command line, mongo shell:
> var doc = { 'burger': {
'patty': 'beef',
'cheese': false,
'toppings': [ 'ketchup', 'onions', 'pickles' ]
} }
> db.burgers.insertOne(doc)
{
"acknowledged" : true,
"insertedId" : ObjectId("5f87f68dd039d5cb61ae2251")
}
Note the _id field is created by the MongoDB.
You can use insertMany to insert many documents - an array of JSON documents. For example,
var docs_array = [ {... }, {... }, ... ]
db.burgers.insertMany(docs_array)
When I try the code above, db.burgers.burger.find() returns 0, when I
am expecting it to return 1
You can query the document using the find or findOne methods. The find returns a cursor (with multiple documents) and the findOne always returns one document (or a null if no document is found). For example:
> db.burgers.findOne()
> db.burgers.findOne( { _id: ObjectId("5f87f68dd039d5cb61ae2251") } )
The above queries return the newly inserted document, above.
Your query db.burgers.burger.find() is not correct, I think. If you want only the burger field ( this a nested document) to be printed, then use projection, like this:
> db.burgers.findOne( {}, { burger: 1, _id: 0 } )
{
"burger" : {
"patty" : "beef",
"cheese" : false,
"toppings" : [
"ketchup",
"onions",
"pickles"
]
}
}
Note the projection allows output only the specific fields, and exclude others. In the above query, the _id field is excluded.
If you want to count documents in the collection use this:
> db.burgers.countDocuments({})
Reference:
Insert
Documents
Query
Documents
Query on Nested
Documents
So I am a building a member system and every member has an array points.
But I can't seem to delete an object when a person tries to remove this point.
users(collection) -
member(document) -
"id" . --fields
"username" . --fields
"userevents" . --array
[0]
"id"
"date"
"price"
[1]
"id"
"date"
"price"
deletePoint = (userId, pointId, date, price) => {
let docRef = this.db.collection('users').doc(userId);
docRef.update({
points: this.db.FieldValue.arrayRemove({
date: date,
id: pointId,
price: price,
}),
});
};
this.db is firebase.firestore()
According to the documentation when you're using arrayRemove you have to point directly to the field that you want to remove, not the content within said field so your example would become:
deletePoint = (userId, pointId, date, price) => {
let docRef = this.db.collection('users').doc(userId);
docRef.update({
points: this.db.FieldValue.arrayRemove("0"),
});
};
And this will delete the whole content of the field (id, date, price) with an index of 0.
https://codesandbox.io/s/stupefied-volhard-jntyu?fontsize=14&hidenavigation=1&theme=dark
this is my codesand box code. on date column result is not showing correctly.
I think the problem with the sort behaviour is that you are trying to sort strings of dates where the year is the last element.
In case you could edit your data directly, try putting the year, the month and so on. Something like:
const data = [
...,
[
"Mason Ray",
"Computer Scientist",
"San Francisco",
39,
142000,
"2019-10-03"
]
Here is an example using date-fnslibrary to parse your input data.
A detail of how is done:
const data = [...].map(info =>
info.map((val, index) => {
const date = new Date(val)
return index === 5
? isValid(date)
? format(date, 'yyyy-MM-dd')
: val
: val
})
)
I have read as many articles as I can on how to correctly update an element in an array in a MongoDB (such as this one: Mongoose, update values in array of objects), and thought I had followed all the advice, but I am still getting this wrong, and would be very grateful if someone could spot my error, as I've been trying to debug this for hours!
The problem I am specifically having is that the findOneAndUpdate call seems to be just updating the first element in the array of "itineraryItems", no matter whether it matches my query for a specific element or not.
Data in my user collection (2 array elements in the itineraryItems array on the user document):
db.users.find({_id: ObjectId("5dd65ce7998d626a2c71a547"), {itineraryItems: 1})
{ "_id" : ObjectId("5dd65ce7998d626a2c71a547"),
"itineraryItems" : [
{ "_id" : ObjectId("5e3d5a301b65f3f9fd1621f8"),
"iType" : "event",
"startDate" : ISODate("2020-02-07T11:00:00Z"),
"endDate" : ISODate("2020-02-07T13:00:00Z"),
"includeTravelTime" : false,
"travelTimeMinutes" : 0,
"item" : null,
"event" : ObjectId("5dea66c182d9ac6fb4c6f36e")
},
{ "_id" : ObjectId("5e3d5a341b65f3f9fd1621ff"),
"iType" : "item",
"startDate" : ISODate("2020-02-07T11:00:00Z"),
"endDate" : ISODate("2020-02-07T13:00:00Z"),
"includeTravelTime" : false,
"travelTimeMinutes" : 0,
"item" : ObjectId("5e29df801f026697b71f7f48"),
"event" : null
}
]
}
My query building function:
Note that queries that satisfy the first if statement (i.e. I pass in a known id for the element in the array) seem to work fine. It is the other 2 cases that seem to fail.
function getUpdateItineraryElementQuery(user, id, type, startDate, endDate, includeTravelTime, travelTimeMinutes, itemId) {
let query = {};
if (
(id!=null) &&
(id!='not_set')
) {
query = {
_id: user._id,
'itineraryItems._id': mongoose.Types.ObjectId(id)
};
} else {
if (type==='event') {
query = {
_id: user._id,
'itineraryItems.iType': type,
'itineraryItems.startDate': startDate,
'itineraryItems.endDate': endDate,
'itineraryItems.includeTravelTime': includeTravelTime,
'itineraryItems.travelTimeMinutes': travelTimeMinutes,
'itineraryItems.event': mongoose.Types.ObjectId(itemId)
};
} else {
if (type==='item') {
query = {
_id: user._id,
'itineraryItems.iType': type,
'itineraryItems.startDate': startDate,
'itineraryItems.endDate': endDate,
'itineraryItems.includeTravelTime': includeTravelTime,
'itineraryItems.travelTimeMinutes': travelTimeMinutes,
'itineraryItems.item': mongoose.Types.ObjectId(itemId)
};
}
}
}
return query;
}
My Mongoose call to findOneAndUpdate:
User.findOneAndUpdate(
query,
{
'itineraryItems.$.startDate': dtNewStartDate,
'itineraryItems.$.endDate': dtNewEndDate,
'itineraryItems.$.includeTravelTime': newIncludeTravelTime,
'itineraryItems.$.travelTimeMinutes': newTravelTimeMinutes
// eslint-disable-next-line no-unused-vars
}, (err, doc) => {
if (err) {
// not found?
res.sendStatus(404).end();
} else {
// ok
res.sendStatus(200).end();
}
}
);
Thanks a lot if you can tell me what I am doing wrong!
You need to match your subdocument using $elemMatch if you want to update only the subdocument that matches all the conditions
Like this,
{
_id: user._id,
'itineraryItems':{
$elemMatch:{
iType: type,
startDate: startDate,
endDate: endDate,
includeTravelTime: includeTravelTime,
travelTimeMinutes: travelTimeMinutes,
item: mongoose.Types.ObjectId(itemId)
}
}
}
There are many good examples of searching multiple string values in LINQ e.g.
public static Product[] GetProducts(Guid[] prodIDs)
{
return (from p in GetProducts()
where prodIDs.Contains(p.ProductID)
select p).ToArray<Product>();
}
I have a list of Products that I need to match from a customer,
but I dont have an exact match - the Customers List Of Products contains my ProductID - but it is not exact - e.g.
Customer MyCompany
Description Description
Prod1XY Prod1
AProd2B Prod2
XXXProd3 Prod3
I thus cannot filter from the prodIDs [string array] because Prod1 does not contain Prod1XY
and thus cannot use the examples that are available.
How can I effectively change (reverse) the working examples
as to search CustomerProducts where it contains my Product Description please?
So to confirm : this is not a duplicate. The examples use the string[] x
input parameter and then searches:
where x.contains
I need help to get it : myProducts.Contains(x)
another online example modified to show the situation:
static void Main(string[] args) {
var table = new[] {
new { uid = 1 },
new { uid = 2 },
new { uid = 3 },
new { uid = 4 },
new { uid = 5 }
};
var stringarray = new[] { "1", "5", "10" };
var results = from xx in table
where table.Contains(stringarray)
select xx;
foreach (var result in results) {
Console.WriteLine("Result: " + result.uid.ToString());
}
}
It is not clear enough what you are trying to accomplish, but under assumption that you want to select all products where ProductID contains any value from specified list, it looks like that it:
public static Product[] GetProducts(string[] prodIDs)
{
return (from p in GetProducts()
where prodIDs.Any(id=>p.ProductID.Contains(id))
select p).ToArray<Product>();
}
Try this
public static Product[] GetProducts(string[] prodIDs)
{
return (
from p in GetProducts()
from q in prodIDs
where p.ProductID.IndexOf(q) > -1
select p)
.ToArray<Product>();
}