【问题标题】:Retrive Data using by $or and $and from two MongoDB collectionDa使用 $or 和 $and 从两个 MongoDB 集合中检索数据
【发布时间】:2016-07-05 13:36:33
【问题描述】:

我想要做的是将 POST 消息的响应显示到我的 webview 中加载的 HTML 上。但是,我的 web 视图显示为空白。我可以通过打印出来在 LogCat 中看到响应消息。但是,我的 webview 再次显示为空白。 Example.html 是我的 webview 中加载的页面。我的实现如下:

  private void startSchedule()
    {
        for(int i=0;i<temPojoData.size();i++)
        {
            tempPojo tem =temPojoData.get(i);
            /////////////////////// Daily and AllDays functionality start here //////////////////
            if(tem.getDaysweekmonth().equals("Daily") )
            {
               if(tem.getDaysbases().equals("AllDays")) {
                   if (findDateBTwoDates(tem.getStartDate(), tem.getEndDate())) {
                       Log.i("Daily Date", "Today Available");
                       layoutID += tem.getLayout();
                   }
               }else if(tem.getDaysbases().equals("Whole Day")){

               }else if(tem.getDaysbases().equals("Morning"))
               {   scheduleStartTimes.add(tem.getStartTime());

               }else if(tem.getDaysbases().equals("After Noon"))
               {   scheduleStartTimes.add(tem.getStartTime());

               }else if(tem.getDaysbases().equals("Evening"))
               {  scheduleStartTimes.add(tem.getStartTime());

               }else if(tem.getDaysbases().equals("Night"))
               {  scheduleStartTimes.add(tem.getStartTime());

               }else if(tem.getDaysbases().equals("Choose Time"))
               {  scheduleStartTimes.add(tem.getStartTime());

               }

            }
            /////////////////////// Weekly and AllDays functionality start here //////////////////
            else if(tem.getDaysweekmonth().equals("weekly") && tem.getDaysbases().equals("AllDays"))
            {
                if(tem.getDaysbases().equals("AllDays")) {

                }
            }
            /////////////////////// Monthly and AllDays functionality start here //////////////////
            else if(tem.getDaysweekmonth().equals("montly") && tem.getDaysbases().equals("AllDays"))
            {

                if(tem.getDaysbases().equals("AllDays")) {

                }
            }

        }
    }

    private void findTimeBTwoTimes(String sTime,String eTime)
    {
        try {
            String string1 = "20:11:13";
            Date time1 = new SimpleDateFormat("HH:mm:ss").parse(string1);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(time1);

            String string2 = "14:49:00";
            Date time2 = new SimpleDateFormat("HH:mm:ss").parse(string2);
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(time2);
            calendar2.add(Calendar.DATE, 1);

            String someRandomTime = "01:00:00";
            Date d = new SimpleDateFormat("HH:mm:ss").parse(someRandomTime);
            Calendar calendar3 = Calendar.getInstance();
            calendar3.setTime(d);
            calendar3.add(Calendar.DATE, 1);

            Date x = calendar3.getTime();
            if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
                //checkes whether the current time is between 14:49:00 and 20:11:13.
                System.out.println(true);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private boolean findDateBTwoDates(String sDate,String eDate)
    {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            String s = sDate.replace(" AM","");
            String e = eDate.replace(" AM","");
            String oeStartDateStr =sDate.replace("PM","");
            String oeEndDateStr =eDate.replace("PM","");
            Log.i("Start End Date  ",sDate+"------------"+eDate);
           Calendar cal = Calendar.getInstance();
            Integer year = cal.get(Calendar.YEAR);

            Date startDate = sdf.parse(oeStartDateStr);
            Date endDate = sdf.parse(oeEndDateStr);
            Date d = new Date();
            String currDt = sdf.format(d);

            if ((d.after(startDate) && (d.before(endDate))) || (currDt.equals(sdf.format(startDate)) || currDt.equals(sdf.format(endDate)))) {
                System.out.println("Date is between 1st april to 14th nov...");
                return true;
            }
            /*else {
                System.out.println("Date is not between 1st april to 14th nov...");

            }*/
        }catch (Exception e){}

        return false;
    }

【问题讨论】:

  • 你的结果集合是不是只包含除了表单_id和empid之外的结果字段?
  • 我的期望是从具有 $or 和 $and 条件的两个集合中获取数据。我的条件是:Name is equal $or:[{Name:'Name-1'},{Name:'Name-2'}] $and result大于300。归档不是问题。
  • Mongo 没有连接。您不能使用查询连接来自两个集合的数据。您必须在应用程序本身中执行此操作,即使用 () 获取所有员工,然后获取他们的 id 并使用 (id and result>300) 作为查找条件对结果集合进行另一次查询。如果 result 是除了 _id 和 empid 之外的唯一字段,为什么不将结果集合嵌入到员工集合本身中呢?
  • 我不知道嵌入概念。你能给出一些想法吗???你能为这个例子写代码吗..
  • 您必须先让我知道您的架构 - 您的结果集合是否仅包含除 _id 和 empid 之外的结果字段?

标签: mongodb mongoid mongodb-query spring-data-mongodb mongodb-java


【解决方案1】:

我将在 sudo 代码中对此进行解释,以便您提出解决方案。

您回答的第一点是 -> 不,您不能以直截了当的方式做到这一点。这背后的原因是 MongoDB 没有/不支持连接。就像在 SQL 中一样,您可以连接两个表并查询它们以获得条件结果;这在 MongoDB 中是不可行的。

但不要失去希望。您不能在数据库端进行连接,但您当然可以在驱动程序/应用程序端提出解决方案。我建议你做的如下。 (我正在使用 JAVA 驱动程序和 Morphia,因此我的解决方案的安排可能看起来特定于它们)

两个集合分别有一个 DAO 接口

public interface MyDAO1 extends DAO<MyClass1, ObjectId>
{
    public MyClass1 getByName(String name);
}
public interface MyDAO2 extends DAO<MyClass2, ObjectId>
{
    public MyClass2 getByResult(int result);
}

首先为上述两个接口创建实现类。这很简单,所以我跳过它继续进行真正的部分。有一个接口的实现

public class MyDAOImpl extends BasicDAO<MyClass, ObjectId> implements MyDAO1, MyDAO2
{
    public MyClass1 getByName (String name)
    {
        MyClass1 output= query collection1 with with the Name;
        return output;
    }
    public MyClass2 getByResult (int result)
    {
        MyClass2 output= query collection2 with with the result;
        return output;
    }
    public void getByNameResult (String name, int result)
    {
        MyClass1 output1 = getByName (name);
        MyClass2 output2 = getByResult (result);
        print them in however format you want OR create a string;
    }
}

请注意:

MyClass1 是员工集合的@Entity 类

MyClass2 是结果集合的@Entity 类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    相关资源
    最近更新 更多